xuefia
Last Updated: January 11, 2017
·
5.569K
· croaky
8e2b996de3842c6ef7e68a82fa5f01f5

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]
Say Thanks
Respond

12 Responses
Add your response

11137

"The more you know..." Thank you for sharing this!

over 1 year ago ·
11140
0 jqfqxfqmioddxv7vu9bmxkvf uf80vavbk1jxkqyy7rkhyjsv ceex8ksx7xyjf94clzii64n4fr

Thanks Dan, it is really helpful.

over 1 year ago ·
11149

Very cool - thank you Dan.

over 1 year ago ·
11151
Img 4302

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 :)

over 1 year ago ·
11167
Fe9752a7c40b4b3c430fbf79ad74f5e2

I'd also like to mention that IntelliJ/RubyMine does this too with a simple shortcut. :)

over 1 year ago ·
11168

Awesome, thanks Dan

over 1 year ago ·
11190

Nice one

over 1 year ago ·
11192
6abd9004f3e9d949b6120ba81496784b

Thanks. I enjoyed this coderwallism.

over 1 year ago ·
11279

That's very helpfull, thanks dude.

over 1 year ago ·
11346
35afc3a

I love it! Introspection FTW.

over 1 year ago ·
11639
809ac34c7d3f932baf08bba11c486b9a

Great

over 1 year ago ·
14003
El48wa69 edited

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.

over 1 year ago ·