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
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/89516/0_JqFQXFqmiodDxV7VU9bMXkvf_uf80VaVBK1JXkqYy7RKhYJsv-cEEX8KSX7xyjf94cLZIi64n4fR.jpeg)
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 :)
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/59836/fe9752a7c40b4b3c430fbf79ad74f5e2.jpeg)
I'd also like to mention that IntelliJ/RubyMine does this too with a simple shortcut. :)
Awesome, thanks Dan
Nice one
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/89152/6abd9004f3e9d949b6120ba81496784b.jpeg)
Thanks. I enjoyed this coderwallism.
That's very helpfull, thanks dude.
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/25608/35afc3a.jpeg)
I love it! Introspection FTW.
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/2937/809ac34c7d3f932baf08bba11c486b9a.jpeg)
Great
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/104981/El48WA69_edited.png)
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.