Sebastian Hungerecker
3/7/2009 5:13:00 PM
Phlip wrote:
> Here's a ragged array:
>
> =A0 [ [ 1 =A0 =A0],
> =A0 =A0 [ 2, 3 ],
> =A0 =A0 [ 4 =A0 =A0],
> =A0 =A0 [ 5, 6 ],
> =A0 =A0 [ 7 =A0 =A0],
> =A0 =A0 [ 8 =A0 =A0] ]
>
> How, with the tightest, or most modern, or coolest statements, can we turn
> it into this?
>
> =A0 [ [ 1, 1 ],
> =A0 =A0 [ 2, 3 ],
> =A0 =A0 [ 4, 4 ],
> =A0 =A0 [ 5, 6 ],
> =A0 =A0 [ 7, 7 ],
> =A0 =A0 [ 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 subar=
ray
is full.
array =3D [ [ 1 ],
[ 2, 3 ],
[ 4 ],
[ 5, 6 ],
[ 7 ],
[ 8 ] ]
size =3D 2
array.map do |subarray|
subarray + [subarray.last] * (size - subarray.size)
end
# =3D> [ [1, 1],
[2, 3],
[4, 4],
[5, 6],
[7, 7],
[8, 8] ]
HTH,
Sebastian