On Wed, Jul 09, 2003 at 02:50:15AM +0900, Bermejo, Rodrigo wrote:
> I would love  to be able to do:
> 
> 
> a=(1..10)
> a.each {| x , y|  p x }
> 
> 1
> 3
> 5
> 7
> 9

For that kind of thing I tend to do the quick-and-dirty

 a = [1,2,3,4,5,6,7,8,9,10]
 while a.size > 0
   p a.shift
   a.shift
 end

A more general pattern might be:

module Enumerable
  def each_group(n=1)
    res = []
    each do |i|
      res << i
      if res.size >= n
        yield res
        res = []
      end
    end
    yield res if res.size > 0
  end
end

By making it part of 'Enumerable' and only depending on 'each', it works for
any enumerable class:

(1..10).each_group(2) { |x,y| puts x }  # prints 1,3,5,7,9

Regards,

Brian.