Merge pull request #12842 from kuldeepaggarwal/array_split

Speed up Array#split when block is passed
This commit is contained in:
Guillermo Iguaran 2013-11-11 00:02:50 -08:00
commit 15ef6bed18

@ -83,10 +83,10 @@ def in_groups(number, fill_with = nil)
#
# [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, &block)
if block
def split(value = nil)
if block_given?
inject([[]]) do |results, element|
if block.call(element)
if yield(element)
results << []
else
results.last << element