Bug #17737
closed`Array#permutation` does not immediately check the arity when no block is given
Description
p [].permutation(1) #=> #<Enumerator: []:permutation(1)>
p [].permutation(1, 3) #=> #<Enumerator: []:permutation(1, 3)>
p [].permutation(1, 3).to_a #=> `permutation': wrong number of arguments (given 2, expected 0..1) (ArgumentError)
p [].cycle(1) #=> #<Enumerator: []:cycle(1)>
p [].cycle(1, 3) #=> `cycle': wrong number of arguments (given 2, expected 0..1) (ArgumentError)
Is this an intentional behavior?
I would expect To raise ArgumentError immediately if the arity is wrong like Array#cycle.
        
           Updated by jeremyevans0 (Jeremy Evans) over 4 years ago
          Updated by jeremyevans0 (Jeremy Evans) over 4 years ago
          
          
        
        
      
      If this is a bug, it doesn't just affect Array#permutation, but many Enumerable methods:
[].each_entry(2)
# => #<Enumerator: []:each_entry(2)>
[].each_entry(2).to_a
# ArgumentError (wrong number of arguments (given 1, expected 0))
[].each_with_index(1)
# => #<Enumerator: []:each_with_index(1)>
[].each_with_index(1).to_a
# ArgumentError (wrong number of arguments (given 1, expected 0))
{}.reverse_each(1)
# => #<Enumerator: {}:reverse_each(1)>
{}.reverse_each(1).to_a
# ArgumentError (wrong number of arguments (given 1, expected 0))
This basically happens for all Enumerable methods that accept a variable number of arguments and use RETURN_SIZED_ENUMERATOR before checking arity.  If we consider this a bug, we should audit the code and fix all cases, not just Array#permutation.
        
           Updated by marcandre (Marc-Andre Lafortune) over 4 years ago
          Updated by marcandre (Marc-Andre Lafortune) over 4 years ago
          
          
        
        
      
      I am not opposed to changes, but I don't believe this actually matters; I can not think of a use-case.
A similar question could be asked of [].permutation(:oops). Should it raise immediately? Asked another way, if the argument responds to to_int, should it be called immediately, or when iterated (or both)?
The ideal is probably that argument checking and conversion be done exactly once and when the enumerator is created, but I don't think it would make a different in the real world.
        
           Updated by matz (Yukihiro Matsumoto) over 4 years ago
          Updated by matz (Yukihiro Matsumoto) over 4 years ago
          
          
        
        
      
      - Status changed from Open to Rejected
I agree with @marcandre (Marc-Andre Lafortune). We need no fix here.
Matz.