Bug #19759
closedSurprising automatic splat with ruby2_keywords_hash
Description
On Ruby 3 and newer the following prints [:aa, :bb, :k]
:
def yielder(splat)
yield([:a, :b], *splat)
end
yielder([[:aa, :bb], Hash.ruby2_keywords_hash({k: :k})]) do |a, b, k:|
p [a, b, k]
end
It expanded the second array involved and [:a, :b]
vanished.
This is surprising because automatic array expansion for blocks
used to only happen when passing one array in total.
The script prints [[:a, :b], [:aa, :bb], :k]
with 2.7.3.
I found this while puzzling over this line in the autosplat logic:
if (given_argc == (NIL_P(keyword_hash) ? 1 : 2) &&
It might be tempting to fix it by reversing part of beae6cbf0fd and make it if (given_argc == 1)
.
However that's incorrect due to inducing a behavior change for cases like
yield(*[[:a, :b], keywords_hash])
.
Updated by jeremyevans0 (Jeremy Evans) over 1 year ago
I submitted a pull request to fix this: https://github.com/ruby/ruby/pull/8039
Updated by jeremyevans (Jeremy Evans) over 1 year ago
- Status changed from Open to Closed
Applied in changeset git|3874381c4483ba7794ac2abf157e265546f9bfa7.
Fix autosplat conditions to handle ruby2_keywords case
Autosplat should not occur if there are two arguments but second
argument is an array containing a ruby2_keywords splat. Only
autosplat if a single argument to be yielded to the block, and there
is no splatted flagged keyword hash passed.
Fixes [Bug #19759]
Updated by jeremyevans0 (Jeremy Evans) over 1 year ago
- Backport changed from 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN to 3.0: UNKNOWN, 3.1: REQUIRED, 3.2: REQUIRED
Updated by nagachika (Tomoyuki Chikanaga) over 1 year ago
- Backport changed from 3.0: UNKNOWN, 3.1: REQUIRED, 3.2: REQUIRED to 3.0: UNKNOWN, 3.1: REQUIRED, 3.2: DONE
ruby_3_2 3354aacb73c65420a10cb41c0696e62dd1ba279b merged revision(s) 3874381c4483ba7794ac2abf157e265546f9bfa7.