我有一个遗留的rails应用程序,在全局命名空间中有很多有趣的(无用的)模块和类。我想知道使用rails c
从哪些文件或宝石中获取它们。
我知道方法存在:.source_location
,__line__
,__file__
和调用者对象,但似乎与确定类或模块的来源无关。
有什么想法吗?谢谢!
使用:
答案 0 :(得分:1)
更多的解决方法而不是真正的解决方案(您要求方法),但我建议使用ri doc。如果已正确生成Rdoc / Ri Doc,则doc会提及源gem。
列出ri所知的所有类:
ri -l
获取特定类或模块的文档:
ri ClassName
获取方法的信息:
ri ClassName#instance_method_name
ri ClassName#class_method_name
如果您愿意,可以使用名为ri_for的gem来让您在运行时检查ri
doc,这将在控制台中为您提供帮助。 irb会话输出示例:
>> require 'ri_for'
=> true
>> String.desc_class
begin RI
String < Object
------------------------------------------------------------------------------
Includes:
Diff::LCS (from gem diff-lcs-1.1.3)
(from gem diff-lcs-1.1.3)
------------------------------------------------------------------------------
Includes Diff::LCS into String.
------------------------------------------------------------------------------
(from gem rake-0.8.7)
------------------------------------------------------------------------------
User defined methods to be added to String.
------------------------------------------------------------------------------
Instance methods:
ext
pathmap
pathmap_explode
pathmap_partial
pathmap_replace
(from gem treetop-1.4.10)
------------------------------------------------------------------------------
Instance methods:
blank?
column_of
indent
line_of
tabto
treetop_camelize
end ri
String
non inherited methods:
%, *, +, <<, <=>, ==, ===, =~, [], []=, ascii_only?, blank?, bytes, bytesize, capitalize, capitalize!, casecmp, center, chars, chomp, chomp!, chop, chop!, chr, clear, codepoints, column_of, concat, count, crypt, delete, delete!, downcase, downcase!, dump, each_byte, each_char, each_codepoint, each_line, empty?, encode, encode!, encoding, end_with?, eql?, force_encoding, getbyte, gsub, gsub!, hash, hex, include?, indent, index, insert, inspect, intern, length, line_of, lines, ljust, lstrip, lstrip!, match, next, next!, oct, ord, partition, replace, reverse, reverse!, rindex, rjust, rpartition, rstrip, rstrip!, scan, setbyte, size, slice, slice!, split, squeeze, squeeze!, start_with?, strip, strip!, sub, sub!, succ, succ!, sum, swapcase, swapcase!, tabto, to_c, to_f, to_i, to_r, to_s, to_str, to_sym, tr, tr!, tr_s, tr_s!, treetop_camelize, unpack, upcase, upcase!, upto, valid_encoding?
non inherited class methods:
try_convert
=> nil
答案 1 :(得分:1)
你无法直接为类/模块找到它 - 但是如果你看一下在类/模块上定义的方法,你可以找到它们的定义位置 - 通过代理,它也是类/的位置模块已定义。
在Pry中你可以去:
[3] (pry) main: 0> stat Set#initialize
Method Information:
--
Name: initialize
Owner: Set
Visibility: private
Type: Unbound
Arity: -1
Method Signature: initialize(enum=?, &block)
Source Location: /Users/john/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/set.rb:67
查看上面的最后一项。