Bug #5747

Proc#curry doesn't always detect too many arguments

Added by Marc-Andre Lafortune over 3 years ago. Updated over 2 years ago.

[ruby-core:41597]
Status:Closed
Priority:Normal
Assignee:Marc-Andre Lafortune
ruby -v:- Backport:

Description

Currently, Proc#curry checks the validity of the arity argument and will raise an error if:

1) arity is less than the number of required arguments or
2) arity is more than the maximum number of arguments
Note that simple Procs always accept any number of arguments (even though they may be ignored), so (2) doesn't applies to them, only to lambda's and procs made from methods.

#2 isn't done as well as it could in case of limited optional parameters:

def Object.foo(a, b=42); end
def Object.bar(a, b);    end

Object.method(:foo).to_proc.curry(3) # => curried proc which will raise an error only when passed it's 3rd parameter
Object.method(:bar).to_proc.curry(3) # => ArgumentError: wrong number of arguments (3 for 2)

Same thing with lambdas

My proposed fix passes SST:
a) usefulness: "fail-early" principle
b) consistency: both methods can only accept a maximum of 2 parameters and are thus treated consistently when attempting to curry with more than 2 arguments
c) intuitiveness and d) performance: similar

It is straightforward (but not obvious), as it passes NIT (but not ODT).


Related issues

Blocked by Ruby trunk - Bug #6085: Treatment of Wrong Number of Arguments Closed 02/25/2012

Associated revisions

Revision 39008
Added by Marc-Andre Lafortune over 2 years ago

  • proc.c (proc_curry): Fix arity check [Bug #5747]

  • test/ruby/test_proc.rb: Test for above

Revision 39008
Added by Marc-Andre Lafortune over 2 years ago

  • proc.c (proc_curry): Fix arity check [Bug #5747]

  • test/ruby/test_proc.rb: Test for above

History

#1 Updated by Koichi Sasada over 3 years ago

  • Target version set to 2.0.0

Any progress on it?

#2 Updated by Marc-Andre Lafortune over 3 years ago

Hi,

Koichi Sasada wrote:

Any progress on it?

I'm having some problems running all tests, but here is what I've done so far.

The first patches defines various min_max_arity functions to get min and max number of (non-ignored) arguments of Proc/Lambdas/Methods. It would make the addition of Ruby methods arity_max or arity_range easy.

Note that to know the maximum arity for most builtin methods would require an extensive refactoring.

The second patch fixes Proc#curry (again, except for most builtin methods). This uses both the first patch and the patches of issue #6089.

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

#3 Updated by Shyouhei Urabe over 3 years ago

  • Status changed from Open to Assigned

#4 Updated by Marc-Andre Lafortune over 2 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r39008.
Marc-Andre, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • proc.c (proc_curry): Fix arity check [Bug #5747]

  • test/ruby/test_proc.rb: Test for above

Also available in: Atom PDF