From: dblack@... Date: 2007-07-10T12:11:14+09:00 Subject: Re: Repacking an array of arrays Hi -- On Tue, 10 Jul 2007, Kaps Lok wrote: > 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. If you have ActiveSupport you can do: array.flatten.in_groups_of(3) with the caveat about flatten that it flattens greedily. If you don't, you can easily get it or roll your own in_groups_of. (It was one of the first things I ever wrote for myself in Ruby, though I think I called it in_slices_of or something.) David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)