Constants in Ruby are surprising when compared to other languages. Let’s walk
through a few examples to clear it up.
Here we define a constant. The name must start with a capital.
LIST=[1,2]pLIST
As with variables, constants are references so their value can be mutated.
LIST<<3pLIST
To protect from mutation, we can freeze the value. Attempting to mutate the
value would then raise FrozenError.
LIST.freeze
Constants can be reassigned, although this emits a runtime warning. Freezing,
being a property of the referenced value, does not protect from reassignment.
LIST=[4,5]pLIST
INCREMENT=1
Contrary to variables, constants traverse method scopes.
defincr(i)i+INCREMENTend
pincr(5)
$ruby examples/constants/constants.rb
[1, 2]
[1, 2, 3]
examples/constants/constants.rb:18: warning: already initialized constant LIST
examples/constants/constants.rb:5: warning: previous definition of LIST was here
[4, 5]
6