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] }