Skip to content

Commit

Permalink
Merge pull request #35498 from sobrinho/fix-including-excluding-flatten
Browse files Browse the repository at this point in the history
Fix including/excluding flattening
  • Loading branch information
kamipo committed Mar 7, 2019
2 parents 134eaca + 8746828 commit 2170338
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
12 changes: 6 additions & 6 deletions activesupport/lib/active_support/core_ext/array/access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ def to(position)

# Returns a new array that includes the passed elements.
#
# Example: [ 1, 2, 3 ].including(4, 5) => [ 1, 2, 3, 4, 5 ]
# [ 1, 2, 3 ].including(4, 5) => [ 1, 2, 3, 4, 5 ]
# [ [ 0, 1 ] ].including([ [ 1, 0 ] ]) => [ [ 0, 1 ], [ 1, 0 ] ]
def including(*elements)
self + elements.flatten
self + elements.flatten(1)
end

# Returns a copy of the Array excluding the specified elements.
#
# people = ["David", "Rafael", "Aaron", "Todd"]
# people.excluding "Aaron", "Todd"
# # => ["David", "Rafael"]
# ["David", "Rafael", "Aaron", "Todd"].excluding("Aaron", "Todd") => ["David", "Rafael"]
# [ [ 0, 1 ], [ 1, 0 ] ].excluding([ [ 1, 0 ] ]) => [ [ 0, 1 ] ]
#
# Note: This is an optimization of <tt>Enumerable#excluding</tt> that uses <tt>Array#-</tt>
# instead of <tt>Array#reject</tt> for performance reasons.
def excluding(*elements)
self - elements.flatten
self - elements.flatten(1)
end

# Alias for #excluding.
Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/core_ext/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def exclude?(object)
# {foo: 1, bar: 2, baz: 3}.excluding :bar
# # => {foo: 1, baz: 3}
def excluding(*elements)
elements.flatten!
elements.flatten!(1)
reject { |element| elements.include?(element) }
end

Expand Down
2 changes: 2 additions & 0 deletions activesupport/test/core_ext/array/access_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ def test_specific_accessor
def test_including
assert_equal [1, 2, 3, 4, 5], [1, 2, 4].including(3, 5).sort
assert_equal [1, 2, 3, 4, 5], [1, 2, 4].including([3, 5]).sort
assert_equal [[0, 1], [1, 0]], [[0, 1]].including([[1, 0]])
end

def test_excluding
assert_equal [1, 2, 4], [1, 2, 3, 4, 5].excluding(3, 5)
assert_equal [1, 2, 4], [1, 2, 3, 4, 5].excluding([3, 5])
assert_equal [[0, 1]], [[0, 1], [1, 0]].excluding([[1, 0]])
end

def test_without
Expand Down
1 change: 1 addition & 0 deletions activesupport/test/core_ext/enumerable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ def test_exclude?
def test_excluding
assert_equal [1, 2, 4], GenericEnumerable.new((1..5).to_a).excluding(3, 5)
assert_equal [3, 4, 5], GenericEnumerable.new((1..5).to_a).excluding([1, 2])
assert_equal [[0, 1]], GenericEnumerable.new([[0, 1], [1, 0]]).excluding([[1, 0]])
assert_equal [1, 2, 4], (1..5).to_a.excluding(3, 5)
assert_equal [1, 2, 4], (1..5).to_set.excluding(3, 5)
assert_equal({ foo: 1, baz: 3 }, { foo: 1, bar: 2, baz: 3 }.excluding(:bar))
Expand Down

0 comments on commit 2170338

Please sign in to comment.