[ruby-core:103085] [Ruby master Bug#17737] `Array#permutation` does not immediately check the arity when no block is given
From:
merch-redmine@...
Date:
2021-03-29 22:15:38 UTC
List:
ruby-core #103085
Issue #17737 has been updated by jeremyevans0 (Jeremy Evans).
If this is a bug, it doesn't just affect `Array#permutation`, but many `Enumerable` methods:
```ruby
[].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`.
----------------------------------------
Bug #17737: `Array#permutation` does not immediately check the arity when no block is given
https://bugs.ruby-lang.org/issues/17737#change-91148
* Author: kachick (Kenichi Kamiya)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin20]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
```ruby
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)
```
```ruby
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.
PR: https://github.com/ruby/ruby/pull/4267
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>