Reputation: 4391
In Ruby is there a way to tell where a method is defined? I'm going through the ruby-guides and there is a line of code that reads Post.all
How can I tell where all
is defined?
Upvotes: 7
Views: 178
Reputation: 21690
If you want to know the file and line where the method is defined, use
Post.method(:all).source_location
It will give you [file, line]
or nil
if it's a C method.
Upvotes: 18
Reputation: 28554
A method can be used via a Method object. Which at that point as an owner
attribute. So you can do something like this:
puts Post.method(:all).owner
That will tell you the module/class that defines the method.
Upvotes: 7