aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-03-07 09:33:21 +0900
committerGitHub <noreply@github.com>2019-03-07 09:33:21 +0900
commit21703382393c87212c27c988420ee5c133c1aa9f (patch)
tree7d4c19df98256b8c184c45bfa04db9c6655dd0f2 /activesupport
parent134eaca7e2f32651eb5bbbcbea8fb990bff22b08 (diff)
parent87468284d523d5e760445633666232840777e312 (diff)
downloadrails-21703382393c87212c27c988420ee5c133c1aa9f.tar.gz
rails-21703382393c87212c27c988420ee5c133c1aa9f.tar.bz2
rails-21703382393c87212c27c988420ee5c133c1aa9f.zip
Merge pull request #35498 from sobrinho/fix-including-excluding-flatten
Fix including/excluding flattening
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/array/access.rb12
-rw-r--r--activesupport/lib/active_support/core_ext/enumerable.rb2
-rw-r--r--activesupport/test/core_ext/array/access_test.rb2
-rw-r--r--activesupport/test/core_ext/enumerable_test.rb1
4 files changed, 10 insertions, 7 deletions
diff --git a/activesupport/lib/active_support/core_ext/array/access.rb b/activesupport/lib/active_support/core_ext/array/access.rb
index c9eecc55f9..10e4c6b09d 100644
--- a/activesupport/lib/active_support/core_ext/array/access.rb
+++ b/activesupport/lib/active_support/core_ext/array/access.rb
@@ -31,21 +31,21 @@ class Array
# 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.
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index d6fb89e588..4675c41936 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -125,7 +125,7 @@ module Enumerable
# {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
diff --git a/activesupport/test/core_ext/array/access_test.rb b/activesupport/test/core_ext/array/access_test.rb
index 8f89c3f25c..427b058925 100644
--- a/activesupport/test/core_ext/array/access_test.rb
+++ b/activesupport/test/core_ext/array/access_test.rb
@@ -35,11 +35,13 @@ class AccessTest < ActiveSupport::TestCase
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
diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb
index 4e9cf3848d..381b5a1f32 100644
--- a/activesupport/test/core_ext/enumerable_test.rb
+++ b/activesupport/test/core_ext/enumerable_test.rb
@@ -220,6 +220,7 @@ class EnumerableTests < ActiveSupport::TestCase
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))