Issue #13196 has been updated by Olivier Lacan.


Although I find Nobu's patch excellent, it still bothers me that the exception says `expected 0`. 

Keyword arguments do increase a method's arity just like regular arguments, so the message is both confusing *and* disingenuous.

Given the following definition:

```ruby
def explode(code:)
  puts "Boom!"
end
```

And the following call:

```ruby
explode "1234"
```

I would much rather receive the following exception message:

```
ArgumentError: invalid argument "1234" (expected keyword arguments: :code, :foo)
```

First observation, since the keywords are symbols, they should be referred to as symbols  otherwise users may incorrectly try to pass variables named `code` and `foo` to recover from the error. Yes, I know it's silly but that appears to be what the exception message suggests.

Second observation, while it could be unwieldy to display the submitted arguments (especially if there are many and their representation is long), it also makes for a much clearer context for the feedback being given. 

An alternative would be:

```
ArgumentError: 1 unexpected non-keyword argument (expected keyword arguments: :code, :foo)
```

----------------------------------------
Bug #13196: Improve keyword argument errors when non-keyword arguments given
https://bugs.ruby-lang.org/issues/13196#change-63205

* Author: Olivier Lacan
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
Given the following method definition:


```ruby
def explode(code:)
  puts "Boom!"
end
```

If a Ruby user doesn't provide any arguments when calling the `explode` method, the following helpful feedback is given:

```ruby
explode
ArgumentError: missing keyword: code
```

But when a Ruby user mistakenly provides a regular argument, the exception message is obtuse and unhelpful: 

```ruby
explode "1234"
ArgumentError: wrong number of arguments (given 1, expected 0)
```

This does not provide information to properly recover from the error. Worse, it's incorrect. It is not true that the method expected 0 arguments. The method expected 1 keyword argument.

Instead, Ruby should respond something like: 

```ruby
explode "1234"
ArgumentError: missing keyword: code, given "1234" which is not a keyword argument.
```

One could argue that this situation would call for a different error class, perhaps a `KeywordArgumentError` that would inherit from `ArgumentError`, but that would extend the scope of this feature request a bit too far in my mind. 

---Files--------------------------------
missing_kwargs.diff (1.2 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>