Skip to content

Commit

Permalink
Fix defect in Enumerable#many introduced in d862dff
Browse files Browse the repository at this point in the history
This changed `many?` to yield with `element, *args`. The causes elements that are arrays to be destructured which can lead to defective behaviour. Since the introduction of `element, *args` was for readability purposes, we can just use `*args` instead and get the same behaviour without the defect.
  • Loading branch information
andrewn617 committed Jun 6, 2023
1 parent e7be1e7 commit 745976b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
4 changes: 2 additions & 2 deletions activesupport/lib/active_support/core_ext/enumerable.rb
Expand Up @@ -93,8 +93,8 @@ def index_with(default = (no_default = true))
def many?
cnt = 0
if block_given?
any? do |element, *args|
cnt += 1 if yield element, *args
any? do |*args|
cnt += 1 if yield(*args)
cnt > 1
end
else
Expand Down
1 change: 1 addition & 0 deletions activesupport/test/core_ext/enumerable_test.rb
Expand Up @@ -278,6 +278,7 @@ def test_many
assert_equal false, GenericEnumerable.new([ 1, 2 ]).many? { |x| x > 1 }
assert_equal true, GenericEnumerable.new([ 1, 2, 2 ]).many? { |x| x > 1 }
assert_equal true, GenericEnumerable.new([ 1, 2, 3]).each_with_index.many? { |x, i| x == i + 1 }
assert_equal true, GenericEnumerable.new([ [1, 2], [3, 4] ]).many? { |x| x.sum > 1 }
end

def test_many_iterates_only_on_what_is_needed
Expand Down

0 comments on commit 745976b

Please sign in to comment.