TIL, 2018-12-17, Immutability in Ruby
Musings
inch
on recommendations for documentation. It also has CI.deep_freeze
freezes Ruby objects - good to look at the code on how it does that.- Almost every gem has config commands and parsers for those config commands. Really cool to think about.
A Review Of Immutability In Ruby
- When two or more objects use the same piece of mutable data, they all have the ability to break each other in ways that can be hard to debug.
- Standard lib’s
freeze
: prevents further modifications toobj
. ARuntimeError
will be raised if modification is attempted. - This will work on all objects except
BasicObject
. - Reassigning a constant variable will generate a warning, but mutating a constant value will not. You have to actually freeze the constant. Problem is, it’s shallow, so the existing elements can actually still change.
- String literals are also immutable, just like numbers and symbols.
- Recursively freezing an object property?
values
gem