When I want to access the constant CONST
in class Test
in
class Test
CONST = 7
end
from outside of the class, I have to do this:
puts Test::CONST
Why do I get an error when I do this?
puts obj::CONST
If obj
is an object of the Test
class, why do I get an error if I try to access the constant through the object?
Because an instance object and a class object are not the same thing. The namespace exists on the class object, and does not exist on the instance.
You can, however, ask the instance for it's class, then drill into that.
puts obj.class::CONST
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