OneBite.Dev - Coding blog in a bite size

do a for loop in Ruby

Code snippet on how to do a for loop in Ruby

  numbers = [1, 2, 3, 4, 5]
for number in numbers
  puts number
end

This code iterates through the array “numbers” which contains the numbers 1-5. The line “for number in numbers” sets the variable “number” to each element in the array “numbers” in sequence. Then the line “puts number” prints the value of the variable “number”, which temporarily holds one of the elements of “numbers”. The loop continues to the end of the array, thus printing each of the values 1-5. Finally, after all the elements in the array have been printed, the loop ends.

ruby