From: lasitha Date: 2009-03-09T02:54:40+09:00 Subject: Re: how to un-ragged a 2D array? On Sun, Mar 8, 2009 at 10:38 PM, Phlip wrote: > lasitha wrote: > >> needs.map {|x, y| [x, y ||= x] } > > You win! But try just: > >  needs.map{|x, y| [x, y || x] } Doh! And i was feeling so clever just a minute ago :) > (And would needs.map{|*x| [*x, *x].first(2) } work on Ruby 1.9? 1.8.7?) Neither. Doesn't compile on 1.8.7 On 1.9 the splat wraps arrays in another array so we end up with: [[1, 1], [[2, 3], [2, 3]], [4, 4], ...] ~~~~~~~~~~~~~~~~ Best i could manage with explicit use of splat was: needs.map{|x| [*x] }.each {|x| x[1] ||= x[0] } Which, combined with Sebastian's earlier solution, generalises to any target length: needs.map{|x| [*x] }.map {|x| x + [x.last] * (legth - x.length) } > However, I ain't gonna edit my gist just to put it in - you missed the > deadline! (-: pfft. cheers :) lasitha