123456789_123456789_123456789_123456789_123456789_

Class: Method

Relationships & Source Files
Inherits: Object
Defined in: proc.c

Instance Method Summary

Instance Method Details

#eql?(other_meth) ⇒ Boolean #==(other_meth) ⇒ Boolean
Also known as: #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.

#call(args, ...) ⇒ Object #[](args, ...) ⇒ Object
Also known as: #call

Invokes the meth with the specified arguments, returning the method's return value.

m = 12.method("+")
m.call(3)    #=> 15
m.call(20)   #=> 32

#arityFixnum

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

#call(args, ...) ⇒ Object #[](args, ...) ⇒ Object

Alias for #[].

#cloneMethod

Returns a clone of this method.

class A
  def foo
    return "bar"
  end
end

m = A.new.method(:foo)
m.call # => "bar"
n = m.clone.call # => "bar"

#curryProc #curry(arity) ⇒ Proc

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

Alias for #==.

#hashInteger

Returns a hash value corresponding to the method object.

See also Object#hash.

#to_sString #inspectString

Alias for #to_s.

#nameSymbol

Returns the name of the method.

#original_nameSymbol

Returns the original name of the method.

#ownerclass_or_module

Returns the class or module that defines the method.

#parametersArray

Returns the parameter information of this method.

#receiverObject

Returns the bound receiver of the method object.

#source_locationArray, 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_methodMethod

Returns a Method of superclass which would be called when super is used or nil if there is no method on superclass.

#to_procProc

Returns a ::Proc object corresponding to this method.

#to_sString #inspectString
Also known as: #inspect

Returns the name of the underlying method.

"cat".method(:count).inspect   #=> "#<Method: String#count>"

#unbindMethod

Dissociates meth from its current receiver. The resulting ::UnboundMethod can subsequently be bound to a new object of the same class (see ::UnboundMethod).