Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The un-googleable ruby's method method

Tags:

methods

ruby

There is apparently a ruby method called method and I can't find any docs or examples on it so far. Could someone please post a few useful annotated examples?

like image 531
Amit Erandole Avatar asked Mar 08 '26 12:03

Amit Erandole


2 Answers

there is docs about that method in ruby api documentation

http://www.ruby-doc.org/core-1.9.3/Object.html#method-i-method

there is also method_defined? which could be also useful if you need method it self

http://www.ruby-doc.org/core-1.9.3/Module.html#method-i-method_defined-3F

like image 111
Milan Jaric Avatar answered Mar 10 '26 02:03

Milan Jaric


I've mostly used it to look up source location of a specific method. (example run in irb on ruby-1.9.2-p290)

class Thing
  def foo
  end
end

Thing.new.method(:foo).source_location
=> ["(irb)", 2]

Thing.new.method(:foo).owner
=> Thing 
like image 24
ohnoes Avatar answered Mar 10 '26 02:03

ohnoes