Actions
Bug #12466
closedEnumerable should yield multiple values when possible
Bug #12466:
Enumerable should yield multiple values when possible
Description
Some methods in Enumerable and Array yield arrays instead of multiple values, e.g.:
If each_cons would yield multiple values (i.e. yield 1, 2 instead of yield [1, 2]), we could write:
But currently, this results in an exception and we have to provide a block to achieve the above:
No big deal, but it looks a bit cumbersome (in Ruby terms) and I don't see how the current behavior is preferable.
Another example is reverse_each which cripples yielded values:
def each_two_values
return enum_for(__method__) unless block_given?
yield 1, 2
yield 2, 3
yield 3, 4
end
each_cons_values.map(&:quo) #=> [(1/2), (2/3), (3/4)]
each_cons_values.reverse_each.map(&:quo) #=> NoMethodError: undefined method `quo' for [3, 4]:Array
Of course, I can easily provide my own implementation:
module Enumerable
def my_reverse_each
return enum_for(__method__) unless block_given?
map { |*values| values }.reverse.each { |values| yield *values }
end
end
each_cons_values.my_reverse_each.map(&:quo) #=> [(3/4), (2/3), (1/2)]
But shouldn't this be the default behavior?
If an array is actually needed (instead of multiple values), there's Enumerable#each_entry which performs the conversion.
BTW, other methods do yield multiple values:
Actions