It's possible to write this way
class Foo
MY_CONST = 100
end
and it's also possible to change it Foo::MY_CONST = 123
There will be a warning from a Ruby compiler, but anyway a constant will be changed.
So Ruby has no constant values?
It's possible to write this way
class Foo
MY_CONST = 100
end
and it's also possible to change it Foo::MY_CONST = 123
There will be a warning from a Ruby compiler, but anyway a constant will be changed.
So Ruby has no constant values?
it depends what kind of action you want to do with your constants.
If you have an
ARRAY = [1,2,3]
#and then
ARRAY << 4
Ruby won't complain.
However, if you
ARRAY = [1,2,3].freeze
#and
ARRAY << 4
#RuntimeError: can't modify frozen Array
You can still
ARRAY = [1,2,3,4]
#warning: already initialized constant ARRAY
If you freeze
FOO
, then trying to reassign FOO::MY_CONST
will create a RuntimeError.
class FOO
MY_CONST = 100
end
FOO.freeze
FOO::MY_CONST = 123
gives
RuntimeError: can't modify frozen Class
They are semantically constants, so you can expect people not to change them. I'd call them liberal constants, see http://pastie.org/4608297