Issue #13196 has been updated by olivierlacan (Olivier Lacan).

File missing_kwargs.diff added

stomar (Marcus Stollsteimer) wrote:
> * I think `rb_str_cat_cstr(mesg, ": ");` in the while-loop doesn't work; ":" must be added before the loop, in the loop only " " to separate different entries

Woops, good catch. Fixed in a new attached patch.

> * suggestion: s/keyword/keyword argument/, since "keyword" might be confused with language keywords like `def`, `end`, ...

Can't do that since the normal kwargs error is:
> ArgumentError: missing keywords: code, token

> Regarding `code` vs. `:code`, IMHO for beginners it's much easier to understand without leading ":", similar to the usage in the call sequence, it's _not_ `explode(:code: 123)` but `explode(code: 123)`, and in the method body.
I've changed my mind on this and I agree. Especially since the above existing error lists references the keyword arguments without colons.

duerst (Martin Drst) wrote: 
> Yes, actually, if a colon is needed at all, I'd put it at the end of the keyword(s), because that's how it appears in the method invocation:

Looks a bit odd, doesn't it? I could be convinced but this is beyond the scope of this patch and issue since there's existing error messages using no colons at all. 

Here's the patch tested on trunk (2.5.0 dev): 

```
irb(main):001:0> def explode(code:,token:)
irb(main):002:1> puts "Boom!"
irb(main):003:1> end
=> :explode
irb(main):004:0> explode
ArgumentError: missing keywords: code, token
	from (irb):1:in `explode'
	from (irb):4
	from /usr/local/bin/irb:11:in `<main>'
irb(main):005:0> explode "1234"
ArgumentError: wrong number of arguments (given 1, expected 0; required keywords: code, token)
	from (irb):1:in `explode'
	from (irb):5
	from /usr/local/bin/irb:11:in `<main>'
irb(main):006:0> RUBY_VERSION
=> "2.5.0"
```


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

* Author: olivierlacan (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)
missing_kwargs.diff (1.23 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>