Show definition of a method at runtime in Ruby
Many people know about method(:foo).source_location
to find where a method is defined at runtime. I just found a better way by using pry.
From the pry console, run:
[8] pry(main)> show-source Bar.scoped.where
From: /Users/arturo/.rvm/gems/ruby-2.2.6/gems/activerecord-3.2.22.1/lib/active_record/relation/query_methods.rb @ line 132:
Owner: ActiveRecord::QueryMethods
Visibility: public
Number of lines: 7
def where(opts, *rest)
return self if opts.blank?
relation = clone
relation.where_values += build_where(opts, rest)
relation
end
Happy Hacking!
Written on April 2, 2016 by arturopie