Map an Array With Index in Ruby
-
Combine
map
andeach_with_index
Methods in Ruby -
Combine
map
andwith_index
Methods in Ruby -
Use
map
Together With the Ruby Range

The map
method is quite handy in Ruby, and it comes in handy when you need to transform an array into another one. For example, the code below uses map
to convert an array of small letters into an array of capital letters.
Example code:
small_letters = ['a', 'b', 'c', 'd']
capital_letters = small_letters.map { |l| l.capitalize }
puts capital_letters
Output:
["A", "B", "C", "D"]
In the above example, say we want to map and get the respective index of each of the letters in the array. How do we do that? Listed below are different ways to achieve that.
Combine map
and each_with_index
Methods in Ruby
The each_with_index
is a Ruby enumerable method.
It does what the name indicates. It iterates through an array or hash and extracts each element with its respective index. Calling map
on the result of each_with_index
gives us access to the indexes.
Here is the official documentation of each_with_index
for more explanation.
Example code:
small_letters = ['a', 'b', 'c', 'd']
numbered_capital_letters = small_letters.each_with_index.map { |l, i| [i + 1, l.capitalize] }
puts numbered_capital_letters
Output:
[[1, "A"], [2, "B"], [3, "C"], [4, "D"]]
Combine map
and with_index
Methods in Ruby
The with_index
is similar to each_with_index
but with slight differences.
Unlike each_with_index
, with_index
cannot be called directly on an array. The array needs to be converted explicitly to an Enumerator first.
Since map
automatically handles the conversion, the execution order matters here, invoke .map
before calling .with_index
.
Example code:
small_letters = ['a', 'b', 'c', 'd']
numbered_capital_letters = small_letters.map.with_index { |l, i| [i + 1, l.capitalize] }
puts numbered_capital_letters
Output:
[[1, "A"], [2, "B"], [3, "C"], [4, "D"]]
It’s also worth mentioning that with_index
accepts an argument that is the index’s offset, where the index should start from. We can make use of that argument instead of having to do i + 1
inside the {}
block.
Example code:
small_letters = ['a', 'b', 'c', 'd']
numbered_capital_letters = small_letters.map.with_index(1) { |l, i| [i, l.capitalize] }
puts numbered_capital_letters
Output:
[[1, "A"], [2, "B"], [3, "C"], [4, "D"]]
Here is the official documentation of with_index
for more explanation.
Use map
Together With the Ruby Range
The Ruby Range is a set of values with a beginning and an end.
For example, 1..10
or 1...11
is a Range that represents a set of values from 1
to 10
. We can use this together with the map
method to achieve the same result as what we have in the examples above.
Example code:
small_letters = ['a', 'b', 'c', 'd']
numbered_capital_letters = (0...small_letters.length).map { |i| [i + 1, small_letters[i].capitalize] }
puts numbered_capital_letters
Output:
[[1, "A"], [2, "B"], [3, "C"], [4, "D"]]
Related Article - Ruby Array
- %i and %I in Ruby
- Combine Array to String in Ruby
- Square Array Element in Ruby
- Merge Arrays in Ruby
- Convert Array to Hash in Ruby
- Remove Duplicates From a Ruby Array