From: Allen Mitchell Date: 2002-10-21T10:07:38+09:00 Subject: Re: Grouping by twos I ran into the same requirement and I solved it by extending the Array class. Have a look... class Array #--------------------------------------------------------------------- # each_group #--------------------------------------------------------------------- def each_group ( sizeof ) limitOf = length-1 0.step( limitOf, sizeof ) { |i| yield( self[i,sizeof] ) } end #--------------------------------------------------------------------- # collect_each_group #--------------------------------------------------------------------- def collect_each_group ( sizeof ) rtn = [] limitOf = length-1 0.step( limitOf, sizeof ) { |i| rtn << yield( self[i,sizeof] ) } rtn end end a = [ 1,2,3,4,5,6,7,8,9,10 ] b = a.collect_each_group(2) { |items| items } and b = [[1,2],[3,4],[5,6],[7,8],[9,10]] Hope it helps... -----Original Message----- From: Martin DeMello [mailto:martindemello@yahoo.com] Sent: October 20, 2002 19:52 To: ruby-talk ML Subject: Grouping by twos Another of those trivial little problems that I obsess about finding the Proper way to do - what's a nice, idiomatic Ruby way to go from [a,b,c,d.....] to [[a,b],[c,d],[e,f],...]? Note that I want to preserve the ordering, so to_hash won't work. One way I thought of was b = [] while a[0] do b << [a.shift, a.shift] end which works but doesn't look particularly elegant. martin