Method: Enumerable#split_between

Defined in:
lib/upm/core_ext.rb

#split_between(&block) ⇒ Object Also known as: cut_between

Split the array into chunks, cutting between two elements.

Example:

[1,1,2,2].split_between{|a,b| a != b } #=> [ [1,1], [2,2] ]


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/upm/core_ext.rb', line 147

def split_between(&block)
  Enumerator.new do |yielder|
    current = []
    last    = nil

    each_cons(2) do |a,b|
      current << a
      if yield(a,b)
        yielder << current
        current = []
      end
      last = b
    end

    current << last unless last.nil?
    yielder << current
  end
end