Ads by ProfitSence
Close

Iterate through array ruby Code Example

Last Updated on Wednesday 5th Oct 2022

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