As the title says, it was personally convenient to start the subscript with an arbitrary number when repeating an array with each_with_index in Ruby, so I will leave it as it is.
Just change ʻeach_with_index to ʻeach.with_index (n)
.
Specify the start number for n.
To start the subscript from 1, write as follows.
juices = ["tea", "cola", "coffee"]
juices.each.with_index(1) do |juice, i|
puts "#{i}Third:#{juice}"
end
First: tea
Second: cola
Third: coffee
With ʻeach_with_index, it is necessary to set ʻi + 1
, so this method is convenient.
Recommended Posts