On Tue, Apr 10, 2001 at 04:08:27PM +0900, Luigi Ballabio wrote:
> At 12:40 PM 4/10/01 +0900, you wrote:

[...]

> >Is there a Ruby method for arrays that will determine if all of the elements
> >are in a sequence?

[...]

> >[1, 3, 5, 7].sequence?(2) -> true
> >[1, 5, 7, 9].sequence?(2) -> false
> >
> >["a", "c", "e", "g"].sequence?(2) -> true
> >["a", "e", "g", "i"].sequence?(2) -> false


Here's a version that will support this:

class Array
    def seq? (n = 1)
        begin
            a = Range.new(*indices(0,-1)).to_a
            b = []
            0.step(a.length - 1, n) {|x| b << a[x] }
            b == self
       rescue
            false
       end
    end
end

It will return false if a range can't be constructed (type mismatch,
succ method undefined for object, etc.). If you want to catch those
errors yourself, remove the rescue clause.

Tyler