Project

General

Profile

Actions

Bug #19759

closed

Surprising automatic splat with ruby2_keywords_hash

Added by alanwu (Alan Wu) 11 months ago. Updated 10 months ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin22]
[ruby-core:114103]

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]).

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0