I'd like to implement an iterator that returns all ranges of
successive elements of an array. If an array consists of [1,2,3,5,6]
the iterator should return 1,3 and 5,6. I don't care whether the
iterator returns an array with two elements or two values. So far I
have this:

#----------------------------------------
x =  [ 1, 2, 3, 4, 7, 8, 12, 15, 16, 22 ]

class Array
  def each_cont_range
    r0 = 0
    for i in 0..length()-2
      if (self[i+1] != self[i].succ)
        yield(self[r0], self[i])
        r0 = i+1
      end
    end
    yield(self[r0], self[-1])
  end
end

x.each_cont_range { |r0, r1| print "range #{r0} to #{r1}\n" }
#---------------------------------------------------

It works but it isn't pretty. I tried to use an inject function but
didn't get anywhere. Suggestions for improvement? Are there any
built-in ways of doing this?

Thanks,
Andrew Queisser