On Tue, 28 Mar 2006 04:59:00 +0900, Farrel Lifson <farrel.lifson / gmail.com>
wrote:

> Hi folks,
> 
> Is there an easy way to get [1,2,3,4,5,6,7,8] into
> [[1,2],[3,4],[5,6],[7,9]] or [[1,2,3,4],[5,6,7,8]] depending on a
> parameter?

From 'enumerator.rb' , #each_slice(n) lets you iterate over successive
slices, and #enum_slice(n) generates an enumerator which you can use
the #to_a on to get the array of slices:

   require 'enumerator'
   a = [1,2,3,4,5,6,7,8,9]
   a.enum_slice(2).to_a   # => [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
   a.enum_slice(4).to_a   # => [[1, 2, 3, 4], [5, 6, 7, 8], [9]]

andrew

-- 
Andrew L. Johnson   http://www.siaris.net/
      What have you done to the cat? It looks half-dead.
          -- Schroedinger's wife