From: "jeremyevans0 (Jeremy Evans) via ruby-core" Date: 2024-04-22T06:20:19+00:00 Subject: [ruby-core:117641] [Ruby master Bug#20442] Printing class variable inconsistent when using nested hashes Issue #20442 has been updated by jeremyevans0 (Jeremy Evans). Status changed from Open to Rejected Not a bug. For the behavior you want, in `Mock4` ```ruby @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`. ---------------------------------------- Bug #20442: Printing class variable inconsistent when using nested hashes https://bugs.ruby-lang.org/issues/20442#change-108052 * Author: k776 (Kieran Pilkington) * Status: Rejected * ruby -v: 3.3.0 * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- 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-------------------------------- test.rb (820 Bytes) -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/