aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorlvl0nax <lvl0nax@gmail.com>2016-05-06 19:15:51 +0300
committerlvl0nax <lvl0nax@gmail.com>2016-05-06 19:15:51 +0300
commit9262e493d56610d64fe132097f70b3e8e2033b59 (patch)
tree7ba7ab841492cdbadb4d7a5ee17b8e716fd94a10 /activesupport
parentfb898e986f8da1943e293e9c42d8c2e1aa4525d6 (diff)
downloadrails-9262e493d56610d64fe132097f70b3e8e2033b59.tar.gz
rails-9262e493d56610d64fe132097f70b3e8e2033b59.tar.bz2
rails-9262e493d56610d64fe132097f70b3e8e2033b59.zip
Array#split refactoring for case with block
Calculating ------------------------------------- before 26.319k i/100ms after 29.414k i/100ms ------------------------------------------------- before 350.623k (± 1.6%) i/s - 1.763M after 416.227k (± 1.4%) i/s - 2.088M Comparison: after: 416226.8 i/s before: 350622.8 i/s - 1.19x slower
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/array/grouping.rb17
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