开发者

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

开发者 https://www.devze.com 2023-01-03 20:44 出处:网络
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? (ot

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号