Would it be helpful to have Ruby collection objects such as Hash, Struct, and OpenStruct, include a
freeze_deeply method that freezes not only the object’s properties, but the objects referred to by those instance variables?
For example, even after freezing this struct, the string value can be appended to with
CityLocation = Struct.new(:name, :lat, :long) city1 = CityLocation.new('New York', 0, 0) city1.freeze city1.name << ' City' # does not raise an error
If Struct had a
freeze_deeply method, this could be prevented:
def freeze_deeply freeze to_h.values.each(&:freeze) self end
There’s probably a much better way to get the values to freeze, but this works for illustration purposes. Regarding classes implementing Enumerable, one can of course use
map(&:freeze), but it might encourage immutability in code to elevate this to its own named method
What do you think?