有没有办法从Ruby/Ruby on Rails中的方法中找到方法?Ruby/Rails - 从哪里来的方法被包括在内?
例如,从搜索Rails API我知道:
link_to
来自ActionView::Helpers::UrlHelper
,和来自ActionView::Helpers::TextHelper
pluralize
但是有没有找出在Ruby中的方式本身?即在irb
或Rails控制台中?有没有办法从Ruby/Ruby on Rails中的方法中找到方法?Ruby/Rails - 从哪里来的方法被包括在内?
例如,从搜索Rails API我知道:
link_to
来自ActionView::Helpers::UrlHelper
,和来自ActionView::Helpers::TextHelper
pluralize
但是有没有找出在Ruby中的方式本身?即在irb
或Rails控制台中?你在你不管上下文可以通过使用获得的源位置:
obj.method(:method).source_location
它不会放弃你正是你想要的,但Rails核心开发人员对正确命名空间的事情很好。下面的例子可以从轨控制台上运行:
Time.method(:zone).source_location
["/Users/pete/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/core_ext/time/zones.rb", 9]
然后你可以去Rails source和搜索该文件。提示:在Github上输入't'并开始输入。它会带你到that file,你可以看到它直接在Time类上定义。
是:
@object.method(:method_name)
例如:
@object.method(:pluralize)
您可能也想阅读本文:http://stackoverflow.com/questions/3393096/how-can-i-get-source-code-of-a-methods-dynamically-and-also-which-file -is-this-m – Casper 2012-04-25 22:50:06
而这个:http://stackoverflow.com/questions/175655/how-to-find-where-a-method-is-defined-at-runtime – Casper 2012-04-25 22:51:28