Last Updated: September 27, 2021
·
6.119K
· croaky

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]

11 Responses
Add your response

Thanks Dan, it is really helpful.

over 1 year ago ·

Very cool - thank you Dan.

over 1 year ago ·

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 ·

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

over 1 year ago ·

Awesome, thanks Dan

over 1 year ago ·

Nice one

over 1 year ago ·

Thanks. I enjoyed this coderwallism.

over 1 year ago ·

That's very helpfull, thanks dude.

over 1 year ago ·

I love it! Introspection FTW.

over 1 year ago ·

Great

over 1 year ago ·

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 ·