提交 77a6f352 编写于 作者: R Rafael França

Merge pull request #24889 from lvl0nax/as_array_split_refactoring

Array#split refactoring for case with block
......@@ -89,24 +89,19 @@ 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)
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册