Class: Method
Instance Method Summary
-
#==(other_meth) ⇒ Boolean
(also: #eql?)
Two method objects are equal if they are bound to the same object and refer to the same method definition and their owners are the same class or module.
-
#[](args, ...) ⇒ Object
(also: #call)
Invokes the meth with the specified arguments, returning the method's return value.
-
#arity ⇒ Fixnum
Returns an indication of the number of arguments accepted by a method.
-
#call(args, ...) ⇒ Object
Alias for #[].
-
#clone ⇒ Method
Returns a clone of this method.
-
#curry ⇒ Proc
Returns a curried proc based on the method.
-
#eql?(other_meth) ⇒ Boolean
Alias for #==.
-
#hash ⇒ Integer
Returns a hash value corresponding to the method object.
-
#inspect ⇒ String
Alias for #to_s.
-
#name ⇒ Symbol
Returns the name of the method.
-
#original_name ⇒ Symbol
Returns the original name of the method.
-
#owner ⇒ class_or_module
Returns the class or module that defines the method.
-
#parameters ⇒ Array
Returns the parameter information of this method.
-
#receiver ⇒ Object
Returns the bound receiver of the method object.
-
#source_location ⇒ Array, Fixnum
Returns the Ruby source filename and line number containing this method or nil if this method was not defined in Ruby (i.e.
-
#super_method
Returns a
Method
of superclass, which would be called when super is used. -
#to_proc ⇒ Proc
Returns a ::Proc object corresponding to this method.
-
#to_s ⇒ String
(also: #inspect)
Returns the name of the underlying method.
-
#unbind ⇒ Method
Dissociates meth from its current receiver.
Instance Method Details
#eql?(other_meth) ⇒ Boolean
#==(other_meth) ⇒ Boolean
Also known as: #eql?
Boolean
#==(other_meth) ⇒ Boolean
Two method objects are equal if they are bound to the same object and refer to the same method definition and their owners are the same class or module.
Also known as: #call
#arity ⇒ Fixnum
Returns an indication of the number of arguments accepted by a method. Returns a nonnegative integer for methods that take a fixed number of arguments. For Ruby methods that take a variable number of arguments, returns -n-1, where n is the number of required arguments. For methods written in C, returns -1 if the call takes a variable number of arguments.
class C
def one; end
def two(a); end
def three(*a); end
def four(a, b); end
def five(a, b, *c); end
def six(a, b, *c, &d); end
end
c = C.new
c.method(:one).arity #=> 0
c.method(:two).arity #=> 1
c.method(:three).arity #=> -1
c.method(:four).arity #=> 2
c.method(:five).arity #=> -3
c.method(:six).arity #=> -3
"cat".method(:size).arity #=> 0
"cat".method(:replace).arity #=> 1
"cat".method(:squeeze).arity #=> -1
"cat".method(:count).arity #=> -1
Alias for #[].
#clone ⇒ Method
Returns a curried proc based on the method. When the proc is called with a number of arguments that is lower than the method's arity, then another curried proc is returned. Only when enough arguments have been supplied to satisfy the method signature, will the method actually be called.
The optional arity argument should be supplied when currying methods with variable arguments to determine how many arguments are needed before the method is called.
def foo(a,b,c)
[a, b, c]
end
proc = self.method(:foo).curry
proc2 = proc.call(1, 2) #=> #<Proc>
proc2.call(3) #=> [1,2,3]
def vararg(*args)
args
end
proc = self.method(:vararg).curry(4)
proc2 = proc.call(:x) #=> #<Proc>
proc3 = proc2.call(:y, :z) #=> #<Proc>
proc3.call(:a) #=> [:x, :y, :z, :a]
#eql?(other_meth) ⇒ Boolean
#==(other_meth) ⇒ Boolean
Boolean
#==(other_meth) ⇒ Boolean
Alias for #==.
#hash ⇒ Integer
Returns a hash value corresponding to the method object.
See also Object#hash.
Alias for #to_s.
#name ⇒ Symbol
Returns the name of the method.
#original_name ⇒ Symbol
Returns the original name of the method.
#owner ⇒ class_or_module
Returns the class or module that defines the method.
#parameters ⇒ Array
Returns the parameter information of this method.
#receiver ⇒ Object
Returns the bound receiver of the method object.
#source_location ⇒ Array, Fixnum
Returns the Ruby source filename and line number containing this method or nil if this method was not defined in Ruby (i.e. native).
#super_method
Returns a Method
of superclass, which would be called when super is used.
#to_proc ⇒ Proc
Returns a ::Proc object corresponding to this method.
Also known as: #inspect
Returns the name of the underlying method.
"cat".method(:count).inspect #=> "#<Method: String#count>"
#unbind ⇒ Method
Dissociates meth from its current receiver. The resulting ::UnboundMethod can subsequently be bound to a new object of the same class (see ::UnboundMethod).