From: John Kaurin Date: 2001-04-19T09:40:09+09:00 Subject: [ruby-talk:13834] All In A Row? I made some modifications to the code supplied by Pete Kernan [ruby-talk 13597]. Hope this is of interest. # Re: [ruby-talk 13589, 13593, 13594, 13597] # Determines if an array's elements are in sequential # order or the order specified by an optional block. class Array def seq? return true if length <= 1 if block_given? (0..length-2).to_a.each do |i| return false unless yield (self[i]) == self[i + 1] end true else (0..length-2).to_a.each do |i| return false unless self[i].next == self[i + 1] end true end end end if __FILE__ == $0 puts puts "Checking Array#seq? ..." a = [1, 2, 3, 4, 5] p a.seq? # True p a.seq? {|x| x + 1} # True p a.seq? {|x| x + 2} # False b = [1, 3, 9, 27] p b.seq? # False p b.seq? {|x| x + 3} # False p b.seq? {|x| x * 3} # True c = [1, 0] p c.seq? # False p c.seq? {|x| x - 1} # True p c.seq? {|x| Math.log x} # True d = %w(a b c d e f) p d.seq? # True p d.seq? {|x| x.next} # True p d.reverse.seq? # False puts end