From: Luigi Ballabio Date: 2001-04-10T16:08:27+09:00 Subject: [ruby-talk:13593] Re: All In A Row? At 12:40 PM 4/10/01 +0900, you wrote: >I thought I saw something like this in my early readings on Ruby, but I >cannot find it now. > >Is there a Ruby method for arrays that will determine if all of the elements >are in a sequence? Not that I know, but you can easily add it. >Something like: > >[1, 2, 3, 4].sequence? -> true >[1, 3, 4, 5].sequence? -> false > >["a", "b", "c", "d"].sequence? -> true >["a", "c", "d", "e"].sequence? -> false This can be quickly done with ranges, i.e., class Array def sequence? self == (self[0]..self[-1]).to_a end end >Maybe extrapolated to take an argument: > >[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 This needs just a bit more coding, but you should find it easy enough. Bye, Luigi