Method: Object#method

Defined in:
proc.c

#method(sym) ⇒ Object

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
  end
  def hello()
    "Hello, @iv = #{@iv}"
  end
end

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

l = Demo.new('Fred')
m = l.method("hello")
m.call   #=> "Hello, @iv = Fred"


1455
1456
1457
1458
1459
1460
1461
1462
1463
# File 'proc.c', line 1455

VALUE
rb_obj_method(VALUE obj, VALUE vid)
{
    ID id = rb_check_id(&vid);
    if (!id) {
	rb_method_name_error(CLASS_OF(obj), vid);
    }
    return mnew(CLASS_OF(obj), obj, id, rb_cMethod, FALSE);
}