diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-05-06 14:29:33 -0500 |
---|---|---|
committer | Rafael França <rafaelmfranca@gmail.com> | 2016-05-06 14:29:33 -0500 |
commit | 77a6f3529a1d792ff98f940f8fbfc6f8fa7d6f16 (patch) | |
tree | cfa473689c633469b70333f60100d53f3761761a /activesupport | |
parent | 21a3b180f11e49e1cd296aacba3bec8120dc7c2e (diff) | |
parent | 9262e493d56610d64fe132097f70b3e8e2033b59 (diff) | |
download | rails-77a6f3529a1d792ff98f940f8fbfc6f8fa7d6f16.tar.gz rails-77a6f3529a1d792ff98f940f8fbfc6f8fa7d6f16.tar.bz2 rails-77a6f3529a1d792ff98f940f8fbfc6f8fa7d6f16.zip |
Merge pull request #24889 from lvl0nax/as_array_split_refactoring
Array#split refactoring for case with block
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/array/grouping.rb | 17 |
1 files changed, 6 insertions, 11 deletions
diff --git a/activesupport/lib/active_support/core_ext/array/grouping.rb b/activesupport/lib/active_support/core_ext/array/grouping.rb index 34af83d1ab..ea9d85f6e3 100644 --- a/activesupport/lib/active_support/core_ext/array/grouping.rb +++ b/activesupport/lib/active_support/core_ext/array/grouping.rb @@ -89,24 +89,19 @@ class Array # [1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]] # (1..10).to_a.split { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]] def split(value = nil) + arr = self.dup + result = [] if block_given? - inject([[]]) do |results, element| - if yield(element) - results << [] - else - results.last << element - end - - results + while (idx = arr.index { |i| yield i }) + result << arr.shift(idx) + arr.shift end else - arr = self.dup - result = [] while (idx = arr.index(value)) result << arr.shift(idx) arr.shift end - result << arr end + result << arr end end |