Project

General

Profile

Actions

Bug #6085

closed

Treatment of Wrong Number of Arguments

Added by marcandre (Marc-Andre Lafortune) about 12 years ago. Updated about 12 years ago.

Status:
Closed
Target version:
ruby -v:
r34800
Backport:
[ruby-core:42906]

Description

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)

Related issues 2 (0 open2 closed)

Related to Ruby master - Bug #6086: Number of arguments and named parametersRejectedmatz (Yukihiro Matsumoto)02/25/2012Actions
Blocks Ruby master - Bug #5747: Proc#curry doesn't always detect too many arguments Closedmarcandre (Marc-Andre Lafortune)12/12/2011Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0