2011-11-08 22 views
4

我有一個傳統的rails應用程序,在全局命名空間中有很多有趣的(對於無用的)模塊和類。我想知道他們使用rails c需要哪些文件或寶石。在運行時,如何檢查模塊或類從哪裏加載?

我知道它存在的方法:.source_location,__line__,__file__和調用者對象,但它似乎無關確定類或模塊的來源。

任何想法?謝謝!

使用:

  • 的Ruby 1.9.2
  • 的Rails 3.1.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 

看的最後一個項目從上面。

1

更多的是解決辦法不能真正解決問題(你問的方法),但我會建議使用ri doc。如果Rdoc/Ri Doc已經正確生成,則該文檔提及源寶石。

列出由裏已知的所有類:

ri -l 

工作再上一個特定的類或模塊DOC:

ri ClassName 

工作再上一個方法的信息:

ri ClassName#instance_method_name 
ri ClassName#class_method_name 

如果你想要,有一個叫做ri_for的寶石,可以讓你檢查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 
+1

他已經在使用Pry,它具有'ri_for' gem的所有功能,然後是一些。 – horseyguy

+0

很酷的東西。不知道撬,試試看 –

相關問題