Is it possible to find in which file/line a method was defined in Ruby (MRI)?
Rub开发者_StackOverflow中文版y 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)
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.
This is the best I could find: How to find where a method is defined at runtime?
精彩评论