4

我有一个遗留的 Rails 应用程序,在全局命名空间中有很多有趣的(无用的)模块和类。我想知道他们需要使用哪些文件或宝石rails c

我知道它存在于方法 : .source_location、和调用者对象中__line____file__但确定类或模块的来源似乎无关紧要。

有任何想法吗 ?谢谢!

使用 :

  • 红宝石 1.9.2
  • 导轨 3.1.1
4

2 回答 2

1

比真正的解决方案更多的解决方法(您要求一种方法),但我建议使用ri doc。如果 Rdoc/Ri Doc 已正确生成,则该文档会提及源 gem。

列出 ri 已知的所有类:

ri -l

获取特定类或模块的文档:

ri ClassName

获取有关方法的信息:

ri ClassName#instance_method_name
ri ClassName#class_method_name

如果您愿意,有一个名为ri_for的 gem可让您ri在运行时检查文档,这将在控制台中为您提供帮助。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
于 2011-11-08T19:25:36.003 回答
1

您无法直接为类/模块找到这一点 - 但如果您查看在类/模块上定义的方法,您可以找到它们的定义位置 --- 通过代理,这也是定义类/模块的位置.

你可以去:

[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

从上面看最后一项。

于 2011-11-09T02:28:06.973 回答