4

I have a legacy rails app with a lot of funny (for useless) modules and classes in the global namespace. I want to know from which files or gems they have been required using rails c.

I know it exists for methods : .source_location,__line__,__file__ and the caller object but it seems irrelevant to determine the origin of a class or a module.

Any ideas ? Thanks!

Using :

  • Ruby 1.9.2
  • Pry
  • Rails 3.1.1
Hartator
  • 5,029
  • 4
  • 43
  • 73

2 Answers2

1

more a workaround than a real solution (you asked for a method), but i would advise to use the ri doc. If the Rdoc/ Ri Doc has been properly generated, the doc mentions the source gem.

to list all classes known by ri :

ri -l

to get doc on a specific class or module :

ri ClassName

to get info on a method :

ri ClassName#instance_method_name
ri ClassName#class_method_name

If you want to, there is a gem called ri_for that lets you inspect the ri doc at runtime, which will help you in the console. Example of the output on an irb session:

>> 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
m_x
  • 12,357
  • 7
  • 46
  • 60
1

You can't find this out for classes/modules directly - but if you look at methods defined on the class/module you can find out where they are defined --- which, by proxy, is also where the class/module is defined.

In Pry you can go:

[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

Look at the last item from above.

horseyguy
  • 29,455
  • 20
  • 103
  • 145