I'm trying to get a nested hash to output a different instance of Hash whenever the default value is used.
The code below crashes (on my own raise that checks if the instances are equal):
def reset_region_data
@region_data = Hash.new(Hash.new)
# @region_data = Hash.new{ Hash.new } # Same result as the line above
# @region_data = Hash.new { |hash,new_key| hash[new_key] = {} } # Same problem as the above lines.
end
def foo
reset_region_data
raise if @region_data[0].hash == @region_data[1 * 50 + 1].hash # <<<<< crashes
end
foo
This is weird. So the hash defaults to all of the same instances? But why?
But this code does not:
a = Hash.new(Hash.new())
a[10][10] = 1
a[11][11] = 2
raise if a[10][10].hash == a[11][11].hash
p a[10][10]
And this code also doesn't crash:
a = Hash.new(Hash.new())
a[10] = 1
a[11] = 2
raise if a[10].hash == a[11].hash
p a[10]