Last Updated: February 25, 2016
·
455
· jaturken

How to see, where method is defined, in runtime

Sometimes you want to see, where some method is defined, right from console. For, example, there are two methods touch defined in Rails, ActiveRecord::Callbacks#touch and
ActiveRecord::Persistence#touch. Which implementation works for users? Simple solution is:

> User.first.method(:touch).owner

=> ActiveRecord::Callbacks

You can even see location of source file:

> User.first.method(:touch).source_location

=> ["/home/jaturken/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/callbacks.rb", 257]