What is the easiest way to convert
[x1, x2, x3, ... , xN]
to
[[x1, 2], [x2, 3], [x3, 4], ... , [xN, N+1]]
arraysindexingruby
What is the easiest way to convert
[x1, x2, x3, ... , xN]
to
[[x1, 2], [x2, 3], [x3, 4], ... , [xN, N+1]]
Best Answer
If you're using ruby 1.8.7 or 1.9, you can use the fact that iterator methods like
each_with_index
, when called without a block, return anEnumerator
object, which you can callEnumerable
methods likemap
on. So you can do:In 1.8.6 you can do: