taw
taw

Reputation: 18820

Is it possible to find in which file/line a method was defined in Ruby (MRI)?

Ruby definitely stores such information at runtime, as it is printed in stack traces - but there doesn't seem to be any obvious way of accessing it. Is there a solution? (other than grepping the sources, or passing nonsense arguments to method in hope of getting the stack trace)

Upvotes: 2

Views: 111

Answers (2)

jason.rickman
jason.rickman

Reputation: 15201

In Ruby 1.9, you have Method#source_location:

require 'yaml'    
p YAML.method(:load).source_location => ["C:/Ruby19/lib/ruby/1.9.1/yaml.rb", 132]

It appears there's a request to backport source_location to 1.8 (http://redmine.ruby-lang.org/issues/show/2180), but I'm not sure if/when that will happen.

Upvotes: 3

thomasfedb
thomasfedb

Reputation: 5983

This is the best I could find: How to find where a method is defined at runtime?

Upvotes: 1

Related Questions