Actions
Bug #20442
closedPrinting class variable inconsistent when using nested hashes
Description
See attached test.rb. I have a class with a class variable. The idea is simple, increment the value and then print out the resulting class var.
Mock1
0
1
Mock2
{}
{"a"=>1}
Mock3
{}
{"a"=>1}
Mock4
{}
{}
Mock1 through Mock3 are working as expected. But Mock4 does not, it outputs a blank hash, rather than the expected: {"a"=>{"a"=>1}}
The value is being set. If I replace the print method with @val['a']['a']
is outputs the number 1 as expected.
Files
Updated by jeremyevans0 (Jeremy Evans) 8 months ago
- Status changed from Open to Rejected
Not a bug. For the behavior you want, in Mock4
@val = Hash.new{|h, k| h[k] = Hash.new(0)}
With @val = Hash.new(Hash.new(0))
, the @val['a']
expression returns the Hash.new(0)
default value, but does not set a value in @val
.
Actions
Like0
Like0