How to Find Where Ruby Methods are Defined
While my teammate Ben Orenstein was debugging an exception raised in a view, it became clear that a page_title
method had been defined twice. Grepping the project revealed only one definition. The second must be coming from a gem, but which one?
Ruby's Method#source_location
can answer the question:
method(:page_title).source_location
It revealed the culprit:
["/Users/ben/.rvm/gems/ruby-2.0.0-p353/gems/flutie-1.3.3/app/helpers/page_title_helper.rb", 2]
Written by Dan Croak
Related protips
11 Responses
Thanks Dan, it is really helpful.
Very cool - thank you Dan.
I recommend trying out pry, it has edit
, ?
and $
commands for this and more. Of course, this one is still useful to know where pry is not available :)
I'd also like to mention that IntelliJ/RubyMine does this too with a simple shortcut. :)
Awesome, thanks Dan
Nice one
Thanks. I enjoyed this coderwallism.
That's very helpfull, thanks dude.
I love it! Introspection FTW.
Great
As @hunterboerner said, I would like to share that we can easily locate a method by pressing the Command button (OSX) and simultaneously clicking the left mouse on the method in RubyMine.