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"
#<Proc:0x000055eecda01510 -e:1>
```

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 `<main>'
hash_default.rb:1:in `block in <main>': 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: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>