ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:99348] [Ruby master Bug#16498] Hash#transform_values in 2.7.0 sets new hash's default to old hash's default_proc
       [not found] <redmine.issue-16498.20200110112332.32693@ruby-lang.org>
@ 2020-07-26  9:33 ` nagachika00
  0 siblings, 0 replies; only message in thread
From: nagachika00 @ 2020-07-26  9:33 UTC (permalink / raw
  To: ruby-core

Issue #16498 has been updated by nagachika (Tomoyuki Chikanaga).

Backport changed from 2.5: DONTNEED, 2.6: DONTNEED, 2.7: REQUIRED to 2.5: DONTNEED, 2.6: DONTNEED, 2.7: DONE

ruby_2_7 05c4c88767b54808cd1459a1d6da11179c422cfa merged revision(s) 1b4d406e3a04032b6d01e92b6d184a16945c6ac3.

----------------------------------------
Bug #16498: Hash#transform_values in 2.7.0 sets new hash's default to old hash's default_proc
https://bugs.ruby-lang.org/issues/16498#change-86742

* Author: ptsengineer (Peter Tsengineer)
* Status: Closed
* Priority: Normal
* ruby -v: ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]
* Backport: 2.5: DONTNEED, 2.6: DONTNEED, 2.7: DONE
----------------------------------------
The following is unexpected for me.

```ruby
Hash.new { }.transform_values { }.default
# => #<Proc:0x000055eecda01510 -e:1>
Hash.new { }.transform_values { }[:any]
# => #<Proc:0x0000563a12e35510 -e:1>
Hash.new { :default }.transform_values { }[:any][]
# => :default
```

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 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:

```ruby
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"
```

```
in `block in <main>': undefined method `[]=' for 0:Integer (NoMethodError)
```

That was quite a confusing error message. It took a while to realise why `[]=` was called on `0`.

We also cannot fix this by using `dig` (`puts prices.dig(:mango, 0) || "We're all out of mangoes today"`) because then, we would get:

```
in `dig': Proc does not have #dig method (TypeError)
```




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-07-26  9:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <redmine.issue-16498.20200110112332.32693@ruby-lang.org>
2020-07-26  9:33 ` [ruby-core:99348] [Ruby master Bug#16498] Hash#transform_values in 2.7.0 sets new hash's default to old hash's default_proc nagachika00

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).