From: Sebastian Hungerecker Date: 2009-03-08T02:13:07+09:00 Subject: Re: how to un-ragged a 2D array? Phlip wrote: > Here's a ragged array: > >   [ [ 1    ], >     [ 2, 3 ], >     [ 4    ], >     [ 5, 6 ], >     [ 7    ], >     [ 8    ] ] > > How, with the tightest, or most modern, or coolest statements, can we turn > it into this? > >   [ [ 1, 1 ], >     [ 2, 3 ], >     [ 4, 4 ], >     [ 5, 6 ], >     [ 7, 7 ], >     [ 8, 8 ] ] I'm making the following assumptions: a) we have variable "size" which contains the size of a full subarray b) subarrays should be filled by repeating the last element until the subarray is full. array = [ [ 1 ], [ 2, 3 ], [ 4 ], [ 5, 6 ], [ 7 ], [ 8 ] ] size = 2 array.map do |subarray| subarray + [subarray.last] * (size - subarray.size) end # => [ [1, 1], [2, 3], [4, 4], [5, 6], [7, 7], [8, 8] ] HTH, Sebastian