From: Michael Fellinger Date: 2009-03-08T09:22:47+09:00 Subject: Re: how to un-ragged a 2D array? On Sun, Mar 8, 2009 at 7:13 AM, Phlip wrote: > Ktx! Now, on to Round Two! > > What if the array were even _more_ ragged?? > >   [   1     , >     [ 2, 3 ], >       4     , >     [ 5, 6 ], >       7     , >       8      ] > > The back-story here is: We are asking the user-programmer to enter either > items or couplets of items. Consider the ActiveRecord DSL, where an :include > => item, for example, can be a single item, or an array of items, or a hash > of paired items, or a hash pointing to an array of items, and so on. AR > _could_ interpret :include => item by promoting all its scalars to arrays, > then running a simpler algorithm that only expects arrays. > > In my case, the user-programmer can leave the second 1, 4, 7, & 8 out, as a > convenience, but the algorithm wants this... > >   [ [ 1, 1 ], >     [ 2, 3 ], >     [ 4, 4 ], >     [ 5, 6 ], >     [ 7, 7 ], >     [ 8, 8 ] ] > > ...as an internal convenience. > > My attempt is needs = needs.map{|x| x.is_a?(Array) ? x : [x,x] }, which is > truly icky, but might be the shortest way. Besides this! > >      needs = needs.map{|x| [x,x].flatten[0..1] } > > -- >  Phlip > > needs.map{|e| ([*e] * 2).first(2) } ^ manveru