Array iterate over (each, each_with_index)
- each
- each_with_index
planets = ["Mars", "Jupyter", "Saturn", "Earth"]
planets.each { |planet| puts planet }
# enumerate
planets.each_with_index { |planet, idx|
puts "#{idx}: #{planet}"
}
Press ← or → to navigate between chapters
Press S or / to search in the book
Press ? to show this help
Press Esc to hide this help
planets = ["Mars", "Jupyter", "Saturn", "Earth"]
planets.each { |planet| puts planet }
# enumerate
planets.each_with_index { |planet, idx|
puts "#{idx}: #{planet}"
}