7

Tracking down method definitions in Ruby

 3 years ago
source link: https://jonleighton.name/2012/finding-methods/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Tracking down method definitions in Ruby

20 February 2012

One of my favourite features of Ruby 1.9 is the #source_location method of Proc and Method. Let me explain. Often I am confronted with a large code base (usually Rails), and want to figure out exactly where some method on some object is defined. Rails has many dark corners, and sometimes finding things isn’t entirely straightforward.

A trick that I often use is to get a Method object for the method that I want to find, and then to print out its location. Like this:

class Foo
  def bar
    "omg, where am I?"
  end
end

p Foo.new.method(:bar).source_location # found you!

Recently I was trying to find out where the #flash method of ActionDispatch::TestRequest was defined. It’s not defined in the main files that contain the definitions for ActionDispatch::TestRequest or ActionDispatch::Request (which is the superclass).

So I pulled out my standard tool:

r = ActionDispatch::TestRequest.new
p r.method(:flash).source_location

It didn’t work:

ArgumentError: wrong number of arguments (1 for 0)

It turns out that ActionDispatch::Request has its own method method, which relates to the HTTP method. Hence Ruby’s method method is overridden.

However, all was not lost! Ruby also makes it possible to get a reference to a method definition that isn’t bound to any particular object. This is called an UnboundMethod, and you can’t call it until you bind it to some object. So I was able to get an unbound reference to the original method method and then bind it to my object. Like so:

r = ActionDispatch::TestRequest.new
meth = Object.instance_method(:method)
p meth.bind(r).call(:flash).source_location # found it!

It turns out that the Request class is reopened in actionpack/lib/action_dispatch/middleware/flash.rb and the flash method gets defined there.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK