Issue #6085 has been updated by Marc-Andre Lafortune.


Hi,

On Mon, Mar 12, 2012 at 8:07 AM, Yusuke Endoh <mame / tsg.ne.jp> wrote:
> One concern: I'm afraid if this change affects people who parses
> the message string of WNA. ??What do you think? ??There is not such
> people, is there? ??I don't want to be pedantic, but I can't feel
> sure because I can no longer use Google codesearch... ??Google!!

The error type is part of the language specs, but I feel like error messages are not meant to be parsed and are subject to change. In this particular case, I just checked and Rubinius gives different error messages (ArgumentError: method 'upcase': given 1, expected 0). The changes I propose are also minimal in their approach and make parsing even easier!

> Anyway, I agree that the current is awkward. ??If no one complains,
> I'm positive to import it tentatively.

Thanks. Just let me know after you've looked at it and I'll gladly commit these.

> Off topic. ??Are you interested in improving a keyword argument?
> There is some issues on its implementation, but I have no time to
> work on it :-(

I'm not sure I have the technical skills needed, but I can definitely try to help. In any case I wanted to work on checking for named arguments and giving a better error message in those cases too. What else could I help on?



On Mon, Mar 12, 2012 at 10:31 AM, Yui NARUSE <naruse / airemix.jp> wrote:
> Use one of follwing:
> * https://github.com/marcandre/ruby/compare/rb_arity_check
> * https://github.com/marcandre/ruby/compare/rb_arity_check.diff
> * https://github.com/marcandre/ruby/compare/rb_arity_check.patch

Nice, thanks! I'll provide this kind of link in the future, quite helpful.
----------------------------------------
Bug #6085: Treatment of Wrong Number of Arguments
https://bugs.ruby-lang.org/issues/6085

Author: Marc-Andre Lafortune
Status: Open
Priority: Normal
Assignee: Yusuke Endoh
Category: core
Target version: 2.0.0
ruby -v: r34800


For brevity, let me abbreviate:

    WNA = "wrong number of arguments"

Ruby could provide more accurate information when raising an ArgumentError for WNA.

Example:

  def foo(a, b=42); end
  foo         # => WNA (0 for 1)
  for(1,2,3)  # => WNA (3 for 2)

It would be strictly superior if the message said instead "WNA (0 for 1..2)" and "WNA (3 for 1..2)":
  * more useful as it gives more information at a glance
  * consistent with calling builtin methods:

    "".index        # => WNA (0 for 1..2)
    "".index(1,2,3) # => WNA (3 for 1..2)

Ruby is also not always consistent in its wording when there is a *rest argument:

  Enumerator.new # => WNA (0 for 1+)
  [].insert      # => WNA (at least 1)
  
  File.chown     # => WNA (0 for 2+)
  Process.kill   # => WNA (0 for at least 2)

While reviewing and factorizing all WNA errors, I also found a problematic case:

  "".sub(//)    # => WNA (1 for 1..2)

It would probably less confusing if it said (1 for 2), as the form without a block requires 2 parameters. Same applies to `sub!`

Also, `Module#define_method` could say "WNA (3 for 1)" when it actually accepts only up to 2 arguments.

I've implemented two patches that address all these issues.

The first one improves the error message when calling user methods and lambdas.

The second harmonizes the builtin methods and fixes the few that need to be fixed.

The two commits can be found here:

https://github.com/marcandre/ruby/commits/rb_arity_check

Complete list of changes:
  * Improvements:

    "".sub(//):          WNA (1 for 1..2) => WNA (1 for 2)
      (same with sub)
    Module#define_method: WNA (3 for 1)    => WNA (3 for 1..2)
    exec:                 WNA              => WNA (0 for 1+)
    Hash.new(1){}:        WNA              => WNA (1 for 0)
    instance_eval("", "", 1, 2)
                          WNA instance_eval(...) or instance_eval{...} 
                                           => WNA (4 for 1..3)
      (same with module_eval and class_eval)
    Module#mix:           WNA (4 for 1)    => WNA (4 for 1..3)
    Module#mix, with incorrect arguments: WNA (2 for 1) => wrong arguments

Wording change:
  * Change of language:   WNA (at least 1) => WNA (0 for 1+)
    [].insert
    extend
    "".delete!
    "".count

  * Process.kill:         WNA (0 for at least 2) => WNA (0 for 2+)

Also, builtin functions calling `rb_scan_args` with both optional arguments and a rest argument would generate an error of the form "WNA (0 for 2..3+)". After this patch, this would now read "WNA (0 for 2+)", again for consistency. The only two such cases I found are in `ext/win32ole.c`

In addition to giving a more consistent error handling, these commits pave the way to:
- improved error reporting for parameters with named parameters (forthcoming issue)
- improved checking for Proc#curry (see bug #5747)



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