Project

General

Profile

Actions

Bug #13196

closed

Improve keyword argument errors when non-keyword arguments given

Added by olivierlacan (Olivier Lacan) about 7 years ago. Updated over 6 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:79439]

Description

Given the following method definition:

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:

explode
ArgumentError: missing keyword: code

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

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:

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 olivierlacan (Olivier Lacan), 02/26/2017 03:26 AM
missing_kwargs.diff (1.23 KB) missing_kwargs.diff Updated Patch olivierlacan (Olivier Lacan), 03/16/2017 08:09 AM

Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #14176: Unclear error message when calling method with keyword arguments ClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0