Reputation: 8825
Why is it possible to do the following? I would not expect it to be.
CAD={:hey => {a: [1], b: [2]}}.freeze
CAD.frozen? #=> true
p=CAD[:hey][:a] #=> [1]
p << nil #=> [1, nil]
CAD #=> {:hey=>{:a=>[1, nil], :b=>[2]}}
UPDATE
I found a solution, thanks to the answer: http://blog.flavorjon.es/2008/08/freezing-deep-ruby-data-structures.html
Upvotes: 1
Views: 78
Reputation: 75458
Only the hash object represented by CAD is frozen, but not the other objects referenced on the hash like CAD[:hey][:a]
.
> CAD={:hey => {a: [1], b: [2]}}.freeze
=> {:hey=>{:a=>[1], :b=>[2]}}
> CAD.frozen?
=> true
> CAD[:hey].frozen?
=> false
> CAD[:hey][:a].frozen?
=> false
Upvotes: 5