Simon Strandgaard <0bz63fz3m1qt3001 / sneakemail.com> wrote:
> class Array
>    def ordered?
>        return true if self.empty?
>        a = self.first
>        self[1..-1].each { |b|
>            return false if a > b
>            a = b
>        }
>        true
>    end
> end

This (anti?)pattern always bothers me - surely a 2-element sliding
window is a common enough pattern that we should capture it once and for
all in Enumerable.

It's in Joel VanderWerf's excellent EnumerableTools, but people tend not
to include external packages when writing quick scripts. For instance,
the current script would simply be

class Array
  def ordered?
    each_cluster(2){|i,j| return false if i > j}
    true
  end
end

Personally, I'd like to see an each and an eachn, both taking block
arity into account:

a = *(1..6)
a.eachn {|x,y| p [x,y]} #=> [1,2] [3,4] [5,6] 
a.each  {|x,y| p [x,y]} #=> [1,2] [2,3] [3,4] [4,5] [5,6]

This would also preserve the semantics of 'each' only consuming one
element per iteration.

martin