Iterate through array ruby Code Example

Last Updated On Saturday 4th Dec 2021

Iterate over elements of an array using each

	peoples = ["Adam", "Alice", "Moxley"]

peoples.each { |person| puts person }
	

The Above OutPuts

	Adam
Alice
Moxley
	

Iterate over elements of an array with index using each_with_index

	peoples = ["Adam", "Alice", "Moxley"]
peoples.each_with_index do |people, i|
    puts i
    puts people
    puts "-----"
end
	

The Above OutPuts

	0
Adam
-----
1
Alice
-----
2
Moxley