Each with index and block : each_with_index « Array « Ruby






Each with index and block


['a', 'b', 'c'].each_with_index do |item, index|
  puts "At position #{index}: #{item}"
end
# At position 0: a
# At position 1: b
# At position 2: c

 








Related examples in the same category