From: William James Date: 2007-09-18T01:10:10+09:00 Subject: Re: group array elements in groups of two On Sep 17, 10:35 am, Yukihiro Matsumoto wrote: > Hi, > > In message "Re: group array elements in groups of two" > on Tue, 18 Sep 2007 00:18:09 +0900, Emmanuel Oga writes: > > |A better way to do this? : > | > |arr= [1, 2, 3, 4, 5, 6, 7, 8] > |new= [] > |while !arr.empty? > | elem1, elem2= arr.pop, arr.pop > | new << [elem2, elem1]; > |end > |new.reverse! > | > |new= [[1, 2], [3, 4], [5, 6], [7, 8]] > > require 'enumerator' > arr= [1, 2, 3, 4, 5, 6, 7, 8] > new=arr.to_enum(:each_slice, 2).to_a Nirvana at last! I won a round of golf with Matz! YM: .to_enum(:each_slice, 2).to_a WJ: .enum_slice(2).to_a