From 521318333eaa2654b9ad535c592281ae9efb9113 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 2 Mar 2015 08:46:20 -0800 Subject: [PATCH] Move Array#without from Grouping to Access concern and add dedicated test (relates to #19157) --- .../lib/active_support/core_ext/array/access.rb | 12 ++++++++++++ .../lib/active_support/core_ext/array/grouping.rb | 12 ------------ activesupport/test/core_ext/array/access_test.rb | 4 ++++ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/array/access.rb b/activesupport/lib/active_support/core_ext/array/access.rb index ca66d806ef..3177d8498e 100644 --- a/activesupport/lib/active_support/core_ext/array/access.rb +++ b/activesupport/lib/active_support/core_ext/array/access.rb @@ -27,6 +27,18 @@ class Array end end + # Returns a copy of the Array without the specified elements. + # + # people = ["David", "Rafael", "Aaron", "Todd"] + # people.without "Aaron", "Todd" + # => ["David", "Rafael"] + # + # Note: This is an optimization of `Enumerable#without` that uses `Array#-` + # instead of `Array#reject` for performance reasons. + def without(*elements) + self - elements + end + # Equal to self[1]. # # %w( a b c d e ).second # => "b" diff --git a/activesupport/lib/active_support/core_ext/array/grouping.rb b/activesupport/lib/active_support/core_ext/array/grouping.rb index b3ada47880..87ae052eb0 100644 --- a/activesupport/lib/active_support/core_ext/array/grouping.rb +++ b/activesupport/lib/active_support/core_ext/array/grouping.rb @@ -113,16 +113,4 @@ class Array results end end - - # Returns a copy of the Array without the specified elements. - # - # people = ["David", "Rafael", "Aaron", "Todd"] - # people.without "Aaron", "Todd" - # => ["David", "Rafael"] - # - # Note: This is an optimization of `Enumerable#without` that uses `Array#-` - # instead of `Array#reject` for performance reasons. - def without(*elements) - self - elements - end end diff --git a/activesupport/test/core_ext/array/access_test.rb b/activesupport/test/core_ext/array/access_test.rb index f14f64421d..3f1e0c4cb4 100644 --- a/activesupport/test/core_ext/array/access_test.rb +++ b/activesupport/test/core_ext/array/access_test.rb @@ -27,4 +27,8 @@ class AccessTest < ActiveSupport::TestCase assert_equal array[4], array.fifth assert_equal array[41], array.forty_two end + + def test_without + assert_equal [1, 2, 4], [1, 2, 3, 4, 5].without(3, 5) + end end