How do I find where a constant is defined in Ruby?
There is a better way to do this as of Ruby 2.7, which is Module.const_source_location
.
> Admin.const_source_location(:LIMIT)
#=> ["SOME_PATH/user.rb", 2]
References:
- Ruby 2.7 adds Module#const_source_location
- ruby-core.doc
In ruby, $"
holds all the file names that are loaded via Kernel.load
.
So you could try something like this:
constant = User
$".detect{|load_path|
load_path.include?(constant.to_s.underscore)
}
Note: The method underscore
is part of Rails/ActiveSupport