diff options
author | KD <kd.engineer@yahoo.co.in> | 2013-11-11 11:32:30 +0530 |
---|---|---|
committer | KD <kd.engineer@yahoo.co.in> | 2013-11-11 11:32:30 +0530 |
commit | 133399482c12820c6c96c5e0e6697cdcc0f158e3 (patch) | |
tree | 7ba8809ec749e52023741277930b56c1efc7ac8a /activesupport | |
parent | 140c0c8881d100b0d0bc2a0bce1227ddfbd9c9bd (diff) | |
download | rails-133399482c12820c6c96c5e0e6697cdcc0f158e3.tar.gz rails-133399482c12820c6c96c5e0e6697cdcc0f158e3.tar.bz2 rails-133399482c12820c6c96c5e0e6697cdcc0f158e3.zip |
Array#split preserving the calling array
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/array/grouping.rb | 4 | ||||
-rw-r--r-- | activesupport/test/core_ext/array_ext_test.rb | 18 |
2 files changed, 14 insertions, 8 deletions
diff --git a/activesupport/lib/active_support/core_ext/array/grouping.rb b/activesupport/lib/active_support/core_ext/array/grouping.rb index 37f007c751..c0d6e5b70c 100644 --- a/activesupport/lib/active_support/core_ext/array/grouping.rb +++ b/activesupport/lib/active_support/core_ext/array/grouping.rb @@ -95,9 +95,9 @@ class Array results end else - results, arr = [[]], self + results, arr = [[]], self.dup until arr.empty? - if (idx = index(value)) + if (idx = arr.index(value)) results.last.concat(arr.shift(idx)) arr.shift results << [] diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index 6cd0eb39b7..57722fd52a 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -212,18 +212,24 @@ class ArraySplitTests < ActiveSupport::TestCase end def test_split_with_argument - assert_equal [[1, 2], [4, 5]], [1, 2, 3, 4, 5].split(3) - assert_equal [[1, 2, 3, 4, 5]], [1, 2, 3, 4, 5].split(0) + a = [1, 2, 3, 4, 5] + assert_equal [[1, 2], [4, 5]], a.split(3) + assert_equal [[1, 2, 3, 4, 5]], a.split(0) + assert_equal [1, 2, 3, 4, 5], a end def test_split_with_block - assert_equal [[1, 2], [4, 5], [7, 8], [10]], (1..10).to_a.split { |i| i % 3 == 0 } + a = (1..10).to_a + assert_equal [[1, 2], [4, 5], [7, 8], [10]], a.split { |i| i % 3 == 0 } + assert_equal [1, 2, 3, 4, 5, 6, 7, 8, 9 ,10], a end def test_split_with_edge_values - assert_equal [[], [2, 3, 4, 5]], [1, 2, 3, 4, 5].split(1) - assert_equal [[1, 2, 3, 4], []], [1, 2, 3, 4, 5].split(5) - assert_equal [[], [2, 3, 4], []], [1, 2, 3, 4, 5].split { |i| i == 1 || i == 5 } + a = [1, 2, 3, 4, 5] + assert_equal [[], [2, 3, 4, 5]], a.split(1) + assert_equal [[1, 2, 3, 4], []], a.split(5) + assert_equal [[], [2, 3, 4], []], a.split { |i| i == 1 || i == 5 } + assert_equal [1, 2, 3, 4, 5], a end end |