At run time How do I check where a module or class is loaded from?

I have an outdated rails application with lots of funny (for useless) modules and classes in the global namespace. I want to know from which files or gems they were needed using rails c .

I know that it exists for methods: .source_location , __line__ , __file__ and the calling object, but it seems that it makes no sense to determine the origin of the class or module.

Any ideas? Thanks!

Using:

  • Ruby 1.9.2
  • mount
  • Rails 3.1.1
+4
source share
2 answers

You cannot find this directly for classes / modules, but if you look at the methods defined in the class / module, you can find out where they are defined --- that, by the proxy, there is also a class / module 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.

+1
source

more a workaround than a real solution (you asked for a method), but I would recommend using ri doc . If the Rdoc / Ri Doc was correctly generated, the original stone is mentioned in the document.

to list all classes known as ri:

 ri -l 

To get a document for a specific class or module:

 ri ClassName 

to get information about the method:

 ri ClassName#instance_method_name ri ClassName#class_method_name 

If you want, there is a gem called ri_for , which allows you to check the ri document at runtime, which will help you in the console. Example output for 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 
+1
source

Source: https://habr.com/ru/post/1380227/


All Articles