Project

General

Profile

Actions

Bug #15753

closed

unknown keyword when passing an hash to a method that accepts a default argument and a named argument

Added by localhostdotdev (localhost .dev) over 5 years ago. Updated about 5 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.0dev (2019-03-18 trunk 67296) [x86_64-darwin17] / ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-darwin17]
[ruby-core:92187]

Description

e.g.

>> def a(b = {}, c: 1); p [b, c]; end; a(a: 1)
Traceback (most recent call last):
        3: from (irb):3
        2: from (irb):3:in `rescue in irb_binding'
        1: from (irb):3:in `a'
ArgumentError (unknown keyword: a)

Prettier version:

def do_something(thing = {}, force: true)
  # doing things
end

do_something({ user: { id: 1 } })
# => ArgumentError (unknown keyword: user)

Tested on ruby-head from ~1 month ago and ruby 2.6.2


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #14183: "Real" keyword argumentClosedActions

Updated by jeremyevans0 (Jeremy Evans) over 5 years ago

I believe this is the same issue as #12717. We are trying to address this issue in #14183.

Actions #2

Updated by jeremyevans0 (Jeremy Evans) about 5 years ago

Updated by jeremyevans0 (Jeremy Evans) about 5 years ago

  • Status changed from Open to Closed

The first example should fail, because you are passing an unsupported keyword to a method that accepts a keyword.

With the acceptance of #14183, the second example now submits a warning:

do_something({ user: { id: 1 } })
# (irb):85: warning: The last argument is used as the keyword parameter
# (irb):82: warning: for `do_something' defined here
# ArgumentError (unknown keyword: :user)

In Ruby 3, the second example will pass the hash as the first argument. To get the Ruby 3 behavior with the master branch:

do_something({ user: { id: 1 } }, **(;{}))
# => nil
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0