Ruby on rails 在运行时,如何检查模块或类从何处加载?
我有一个遗留的rails应用程序,在全局名称空间中有许多有趣的(无用的)模块和类。我想知道使用Ruby on rails 在运行时,如何检查模块或类从何处加载?,ruby-on-rails,ruby,ruby-on-rails-3.1,irb,pry,Ruby On Rails,Ruby,Ruby On Rails 3.1,Irb,Pry,我有一个遗留的rails应用程序,在全局名称空间中有许多有趣的(无用的)模块和类。我想知道使用rails c需要哪些文件或gem 我知道它存在于方法:.source\u location、\uuuuuu line\uuuuuuu、\uuuuuu file\uuuuuuu和调用者对象中,但它似乎与确定类或模块的起源无关 有什么想法吗?谢谢 使用: Ruby 1.9.2 窥探 轨道3.1.1 与其说是一个真正的解决方案,不如说是一个变通办法(您要求提供一种方法),但我建议您使用。如果Rdoc/Ri
rails c
需要哪些文件或gem
我知道它存在于方法:.source\u location
、\uuuuuu line\uuuuuuu
、\uuuuuu file\uuuuuuu
和调用者对象中,但它似乎与确定类或模块的起源无关
有什么想法吗?谢谢
使用:
- Ruby 1.9.2
- 窥探
- 轨道3.1.1
- 与其说是一个真正的解决方案,不如说是一个变通办法(您要求提供一种方法),但我建议您使用。如果Rdoc/Ri文档已正确生成,则该文档会提到源gem
要列出国际扶轮已知的所有类别:
ri -l
要获取特定类或模块上的文档,请执行以下操作:
ri ClassName
要获取有关方法的信息,请执行以下操作:
ri ClassName#instance_method_name
ri ClassName#class_method_name
如果您愿意,有一个名为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
>需要“ri\u for”
=>正确
>>String.desc_类
开始RI
字符串<对象
------------------------------------------------------------------------------
包括:
差异::LCS(来自gem Diff-LCS-1.1.3)
(摘自gem diff-lcs-1.1.3)
------------------------------------------------------------------------------
将Diff::LCS包含到字符串中。
------------------------------------------------------------------------------
(摘自gem rake-0.8.7)
------------------------------------------------------------------------------
要添加到字符串的用户定义方法。
------------------------------------------------------------------------------
实例方法:
提取
路径图
路径图\u爆炸
部分路径映射
路径图\u替换
(摘自gem treetop-1.4.10)
------------------------------------------------------------------------------
实例方法:
空白?
第u列
缩进
第二行
塔布托
树顶驼峰
结束ri
一串
非继承方法:
%,*,+,对于类/模块,您无法直接找到这一点,但是如果您查看在类/模块上定义的方法,您可以找到它们的定义位置——通过代理,也就是定义类/模块的位置
在中,您可以:
[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
查看上面的最后一项。他已经在使用Pry,它具有ri_的所有功能,用于gem,然后是一些。