Using pry
, it's very simple to find where a method is defined and see the source by edit-method
command. Yet there is no correspondence for class itself. When that class has no methods defined itself, it's hard to locate the source through pry
.
Classes are constants, so it's equivalent to ask where to find the source in which a particular Ruby constant is defined. Thank you very much.
Ruby Constants Constants begin with an uppercase letter. Constants defined within a class or module can be accessed from within that class or module, and those defined outside a class or module can be accessed globally. Constants may not be defined within methods.
Although constants look like local variables with capital letters, they have the visibility of global variables: they can be used anywhere in a Ruby program without regard to scope.
No need to require ActiveSupport. Usage example: class User; def self. lookup; const_get('SomeClassName);end; end User. lookup will return class itself.
From the docs: Do not define constants within a block, since the block's scope does not isolate or namespace the constant in any way. If you are trying to define that constant once, define it outside of the block instead, or use a variable or method if defining the constant in the outer scope would be problematic.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With