Categories
Uncategorized

How to Tell Which File a Ruby Method Comes From

I wanted to open up a file to examine a method from a Ruby library I was using. A quick way to find out exactly which file the method comes from is to do this (looking up the location of the method JSON.parse):

m = JSON.method(:parse)
puts "File: #{m.source_location}"

or simply:

puts JSON.method(:parse).source_location

This will return the full path and line number like follows:

/Users/steve/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/json/common.rb
145

Handy.