From: Reacher Date: 2008-02-05T01:20:00+09:00 Subject: Re: convert array to array-of-arrays? On Feb 4, 10:00 am, Phlip wrote: > Chris Hulan wrote: > >> Given [1, 2, 3, 4], what's the most Rubiesque way to get [[1, 2], [3, 4]]? > > have a look at enum.partition (http://ruby-doc.org/core/classes/ > > Enumerable.html#M003161) > > I can't partition by value, so the closest conceptual match would be something like: > >     assert do >        [1, 2, 3, 7].partition_with_index{|p,i| i.odd? }.transpose == >             [[1, 2], [3, 7]] >     end > > And that's a heckuva lot of typing, even if partition_with_index existed! > > -- >    Phlip Roll your own? class Array def regroup(count) # do some Ruby awesomeness* end end >> a = [1, 2, 3, 4] [1, 2, 3, 4] >> a.regroup(2) [[1, 2], [3, 4]] * - exercise left up to the reader