A double splatting not work with named arguments, is it a bug?

Following code not work.

	def foo(x, **others)
	  bar(wait: 1, **others)  # => syntax error.
	end

	def bar(wait, **others)
	  p others
	end

	foo x: 1, y: 2, z: 3 # => the expected output: {y: 2, z: 3}

But following code works

	def foo(x, **others)
	  bar(1,  **others)  # => works
	end

	def bar(wait, **others)
	  p others
	end

	foo x: 1, y: 2, z: 3 # => the expected output: {y: 2, z: 3}

This leads to a problem, if foo want to use double splatting to forward arguments to bar,
the bar can not force use named argument, signature like this never work. def bar(*, wait, **others).

is there any reason for the first code is invalid?

Thanks.

For simplicity, you need to splat first, then add extra arguments:

def foo(x, **others)
  bar(**others, wait: 1) # Note the change here
end

def bar(wait, **others)
  p others
end

foo x: 1, y: 2, z: 3
1 Like

Thanks, above code works. i can define bar prefix with a * like bar(*, wait, **others) now.

def foo(*, x, **others)
  bar(**others, wait: 1)
end

def bar(*, wait, **others)
  p others
end

foo x: 1, y: 2, z: 3 # => the expected output: {y: 2, z: 3}

But, i still consider this a little inconsistent, user must invoke bar like bar(**others, wait: 1), but, user can not define method bar like def bar(*, **others, wait).

1 Like