Map with Index
Ever wanted map_with_index
, like each_with_index
except for map
instead of each
? Turns out you can, with just a 1 character change. with_index
is a method on Enumerator
that lets you do just that:
['a', 'b', 'c'].map.with_index do |x, index|
[x, index]
end
#=> [["a", 0], ["b", 1], ["c", 2]]
Tweet