From: duerst@... Date: 2017-02-27T01:50:59+00:00 Subject: [ruby-core:79794] [Ruby trunk Bug#13196] Improve keyword argument errors when non-keyword arguments given Issue #13196 has been updated by Martin D��rst. Marcus Stollsteimer wrote: > 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. 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: ``` ArgumentError: 1 unexpected non-keyword argument (expected keyword arguments: code:, foo:) ``` But I think the colons are unnecessary; the name of the arguments is `code` and `foo`; that these names are expressed as symbols in *some* contexts and that symbols are denoted with colons before or after are syntactic details depending on the context. ---------------------------------------- Bug #13196: Improve keyword argument errors when non-keyword arguments given https://bugs.ruby-lang.org/issues/13196#change-63222 * 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: