Say I have:
class MyClass
MY_ENUM = [MY_VALUE_1 = 'value1', MY_VALUE_2 = 'value2']
end
Something like this is possible:
p MyClass::MY_VALUE_1 #=> "value1"
Why? Isn't MY_VALUE1
and MY_VALUE_2
constant scope inside the []
?
You can access the nested constant MY_VALUE_1
because it is in the same scope as the outer constant MY_ENUM
: Both constants are in the scope of class MyClass
.
You expected the [...]
construct to define a new scope, but it does not. In Ruby, only three things define a new scope:
class SomeName
module SomeName
def some_name
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