Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/classes/array.rb
Instance Method Summary collapse
-
#split(value = nil, &block) ⇒ Object
from rails: github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/array/grouping.rb Divides the array into one or more subarrays based on a delimiting
value
or the result of an optional block.
Instance Method Details
#split(value = nil, &block) ⇒ Object
from rails: github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/array/grouping.rb Divides the array into one or more subarrays based on a delimiting value
or the result of an optional block.
[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]]
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/classes/array.rb', line 8 def split(value = nil, &block) inject([[]]) do |results, element| if block && block.call(element) || value == element results << [] else results.last << element end results end end |