Ruby Source Location
Ruby gives programmers some incredibly helpful tools to help debug and understand their code. One of these is tools is the #source_location
method. Defined in the Method class, #source_location
will point you to the file in which a given method is defined.
I have found this helpful in trying to find the source of Rails internal methods, so I can understand how Rails operates on a deeper level:
user = User.last user.method(:first_name) # returns an instance of the Method class. user.method(:first_name).source_location # ["/usr/local/rvm/gems/ruby-2.1.5/gem/aciverecord-4.1.4/lib/activerecord/attribute_methods.rb", 45]
We get back the location of the file, and even the line number. If you want to get a better understanding of what’s going on behind the scenes, give #source_location
a try.