From: pht24@... Date: 2020-01-10T11:23:33+00:00 Subject: [ruby-core:96759] [Ruby master Bug#16498] Hash#transform_value in 2.7.0 sets new hash's default to old hash's default_proc Issue #16498 has been reported by ptsengineer (Peter Tseng). ---------------------------------------- Bug #16498: Hash#transform_value in 2.7.0 sets new hash's default to old hash's default_proc https://bugs.ruby-lang.org/issues/16498 * Author: ptsengineer (Peter Tseng) * Status: Open * Priority: Normal * Assignee: * Target version: * ruby -v: ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux] * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN ---------------------------------------- The most minimal reproduction I found, in Ruby 2.7: ``` $ ruby -e "puts Hash.new { }.transform_values { }.default" # ``` That was unexpected for me, because I did not think a default_proc would turn into a default. The previous behaviour in 2.6 was that the default would be nil, so this is new in 2.7. But I didn't see this get mentioned in the 2.7 NEWS. May I learn of the proper use for this and/or whether it is intentional? If you wonder when we might see something like this, I had found it with code similar in idea to the following: ``` weights = Hash.new { |h, k| h[k] = [] } weights[:apple] << 1 weights[:apple] << 2 prices = weights.transform_values { :arbitrary } puts (prices[:mango] || [])[0] || "We're all out of mangoes today" ``` ``` $ ruby hash_default.rb Traceback (most recent call last): 1: from hash_default.rb:7:in `
' hash_default.rb:1:in `block in
': undefined method `[]=' for 0:Integer (NoMethodError) Did you mean? [] ``` That was quite a confusing error message, since it took a while to realise why `[]=` has gotten called on 0... -- https://bugs.ruby-lang.org/ Unsubscribe: