The slice_when method in Ruby
Creates an enumerator for each chunked elements. The beginnings of chunks are defined by the block.
This method splits each chunk using adjacent elements, elt_before, and elt_after, in the receiver enumerator. This method split chunks between elt_before and elt_after where the block returns true.
The block is called the length of the receiver enumerator minus one.
The result enumerator yields the chunked elements as an array. So each method can be called as follows:
enum.slice_when { |elt_before, elt_after| bool }.each { |ary| ... }
For example:
Return adjacent elements from this array [1, 2, 3, 5, 6, 9, 10] in chunked elements as an array.
[1, 2, 3, 5, 6, 9, 10].slice_when {|i, j| i+1 != j }.to_a
=> [[1, 2, 3], [5, 6], [9, 10]]