ruby 3 array argument splat with keyword arguments
Asked Answered
B

1

6

Before ruby 3 it was possible to do sth like this

def test a, **o
  p a, o
end
t = [:ok, **{ok: 2}]
test *t

it would properly assign

:ok to a and {ok: 2} to o

invoking in ruby 3

you will get

ArgumentError (wrong number of arguments (given 2, expected 1))

Is there work around to splat array argument that holds keyword argument on second position?

Biannual answered 9/1, 2021 at 13:17 Comment(1)
Does this answer your question? ArgumentError after updating from Ruby 2.7 to Ruby 3.0Hulky
A
0

If you need to send the second parameter as a hash you need to do next:

def test a, o
  p a, o
end
t = [:ok, {ok: 2}]
test *t

because of the separating keyword and positional arguments.

Amarillo answered 20/1, 2023 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.