iterate over an array in Ruby
Code snippet on how to iterate over an array in Ruby
array = [1, 2, 3, 4, 5]
array.each do |num|
puts num
end
This code is iterating over an array of numbers in Ruby. The array is stored in the variable ‘array’ which contains the values 1, 2, 3, 4, and 5. The ‘each’ loop will loop through each item in the array, in this case each number, and store it in the variable ‘num’. The ‘puts’ method will print each number to the console. So this code will print the numbers 1, 2, 3, 4 and 5 to the console, each on a new line.