I want to use a hash as a bigger hash’s default value. But I don’t know how to set the inner hash’s value through the outer hash.
h = Hash.new do
{:counter => 0}
end
h[:a][:counter] += 1
=> 1
h[:a][:counter] += 1
=> 1
h[:a][:counter] += 1
=> 1
h
=> {}
Uh, what’s the right way?
If you initialize with a default value of a hash:
Then you can follow the calling pattern in your example:
Alternatively you may want to intialize with a block so a new instance of the
:counterhash is created every time you use a new key: