From: Eric Hodel <drbrain@...7.net>
Date: 2012-03-13T07:27:08+09:00
Subject: [ruby-core:43255] [ruby-trunk - Bug #6085] Treatment of Wrong Number of Arguments


Issue #6085 has been updated by Eric Hodel.


Much like NameError#name and LoadError#path, why not ArgumentError#expected and ArgumentError#given?  Also, ArgumentError#method could return the method name called.

This eliminates the need for parsing of the error message, but the naming feels wrong for keyword arguments.
----------------------------------------
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/