Project

General

Profile

Actions

Bug #17727

closed

Unexpected syntax error when passing hash to **arg with kwarg

Added by colintherobot (Colin Hart) about 4 years ago. Updated about 4 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin19]
[ruby-core:102907]

Description

Background

Given a method that takes a kwarg and a **positional_arg it throws an unexpected syntax error if you pass it an intact hash. This happens when you call the method, but also when you declare the method if it is multi-line.

def foo(a:, **b)
   [a,b]
end 

Expectation

If you call this method without deconstructing the hash passed to the second argument first I would expect a message indicating that you need to deconstruct the hash first.

Steps to reproduce

Instead it throws a syntax error:

foo(a: '1', {b: 2})
SyntaxError: unexpected ')', expecting =>
foo(a: '1', {b: 2})
                  ^

Passing case when you deconstruct the hash first:

foo(a: '1', **{b: 2})
=> ["1", {:b=>2}]

But wondering if there's something else going on because you get a different error when defining this case on multiple lines. If you're trying to run this in a repl environment it won't even let you complete the method call.

from repl:

foo(
  a: '1',
  {b: 2}
SyntaxError: unexpected '\n', expecting =>

passing case:

foo(
  a: '1',  
  **{b:1}
)  
=> ["1", {:b=>1}]

Attached is a ruby script to reproduce the above cases.


Files

kwarg_bug.rb (291 Bytes) kwarg_bug.rb colintherobot (Colin Hart), 03/17/2021 08:08 PM

Updated by colintherobot (Colin Hart) about 4 years ago

  • Description updated (diff)

Typos/readability

Actions #2

Updated by colintherobot (Colin Hart) about 4 years ago

  • Description updated (diff)

Updated by jeremyevans0 (Jeremy Evans) about 4 years ago

  • Status changed from Open to Rejected

Ruby syntax does not allow positional arguments after keyword arguments. a: '1' is a keyword argument and {b: 1} is a positional argument (**{b: 1} is a keyword argument, which is why that works). So the syntax errors are expected in this case, not a bug.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0