iterate over an array of strings with index using the map function in ruby

main.rb
strings = ["foo", "bar", "baz"]

strings.map.with_index do |string, index|
    "#{index}: #{string}"
end
105 chars
6 lines

This will iterate over the strings array and use the map function to create a new array containing each string with its corresponding index. The with_index method is used to pass the index as an argument to the block. The output would be an array with the following elements:

main.rb
[
    "0: foo",
    "1: bar",
    "2: baz",
]
46 chars
6 lines

related categories

gistlibby LogSnag