Rant: Constants can’t be mutable

I’ve been working with Ruby lately, and a few people have told me that Ruby has constants, but they are mutable. Recently, I’ve been reading a new book on Ruby, and read the following passage:Dcx1I5U

A lot of other places mirror this (e.g. rubylearning.com). You cannot have constants if they are mutable. Mutable constants are just variables. Take a look at the definition of a constant from Wikipedia:

In computer programming, a constant is an identifier with an associated value which cannot be altered by the program during normal execution – the value is constant. This is contrasted with a variable, which is an identifier with a value that can be changed during normal execution – the value is variable.

This is pretty basic stuff to most programmers (and people with common sense). Ruby constants are actually just variables that follow the naming convention for constants.

It’s a stretch, but you could say Ruby has weak constants (I’d prefer to just say that no, ruby does not have constants).

Leave a Reply