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

Bonus from @ulysse-bn:

To have a quick access to this method, you can add it to IRB config like this:

  • ~/.irbrc
# displays path of constant
#
# Usage:
#
#     _source_location(::ActiveRecord::Base)
#
def _source_location(const)
  Object.const_source_location(const.name)
end

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)