method(p1) public

Looks up the named method as a receiver in obj, returning a Method object (or raising NameError). The Method object acts as a closure in obj’s object instance, so instance variables and the value of self remain available.

class Demo
  def initialize(n)
    @iv = n
  def hello()
    "Hello, @iv = #{@iv}"

k =
m = k.method(:hello)   #=> "Hello, @iv = 99"

l ='Fred')
m = l.method("hello")   #=> "Hello, @iv = Fred"
Show source
Register or log in to add new notes.
October 4, 2010
1 thank

Bind the named method to the receiver

Binds the named method to the receiver, returning a Method object with access to the internals of the receiver, such as self and instance variables.