From: James Edward Gray II Date: 2007-07-10T21:44:23+09:00 Subject: Re: Repacking an array of arrays On Jul 10, 2007, at 7:26 AM, dblack@wobblini.net wrote: > Hi -- > > On Tue, 10 Jul 2007, Robert Klemme wrote: > >> 2007/7/10, Kaps Lok : >>> Is there an elegant (maybe a one-liner) for repacking: >>> [[1, 4, 7, 10, 13], [2, 5, 8, 11, 14], [3, 6, 9, 12, 15]] >>> to? >>> [[1, 4, 7], [10, 13, 2], [5, 8, 11], [14, 3, 6], [9, 12, 15]] >>> So rather than having 3 arrays each with 5 elements, I get five >>> arrays >>> of 3 elements each... >>> the only way I can see to do this is by looping the array, and >>> creating >>> new arrays. >> >> Try this: >> >> require 'enumerator' >> p [[1, 4, 7, 10, 13], [2, 5, 8, 11, 14], [3, 6, 9, 12, >> 15]].flatten.to_enum(:each_slice, 3).to_a > > And in 1.9 I guess it would be: > > array.flatten.each_slice(3).to_a > > I'd like to see a method (I don't think there is one) in 1.9 that > would return the sliced-up array without the need for the explict to_a > operation. I suspect the most common use will be exactly that. I bet rolling �_with_index iterators will be pretty common too: enum.each_with_index.inject� James Edward Gray II