From: pete@... (Pete Kernan) Date: 2001-04-11T01:10:14+09:00 Subject: [ruby-talk:13597] Re: All In A Row? On Tue, 10 Apr 2001 03:15:47 GMT, John Kaurin pontificated: + Is there a Ruby method for arrays that will determine if all of the elements + are in a sequence? + + 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 + + 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 when you design your own you could pass it a block class Array def seq? return true if length <= 1 for i in (0..length-2).to_a return false unless yield (self[i]) == self[i+1] end true end end a = [1,2,3,4,5] p a.seq? {|x| x + 1} #true b = [1,3,9,27] p b.seq? {|x| x + 1} #false p b.seq? {|x| x * 3} #true p [1,0].seq? {|x| Math.log x} #true c = %w(a b c d e f) p c.seq? {|x| x.succ} #true -- ruby -F- -e '$, = $; . sub /./, % \ Another\ ; print %w.Just Rewbie .. join'