A flat map would be the best way to do this, it displays (converts), then aligns the list to a single array. Let's say we wanted to add two elements for even numbers:
(1..10).flat_map { |i| i.even? ? [i, i**2] : i }
He will return:
[1, 2, 4, 3, 4, 16, 5, 6, 36, 7, 8, 64, 9, 10, 100]
Compared to the returned card:
[1, [2, 4], 3, [4, 16], 5, [6, 36], 7, [8, 64], 9, [10, 100]]
source
share