OneBite.Dev - Coding blog in a bite size

use a basic loop in Ruby

Code snippet on how to use a basic loop in Ruby

arr = [1,2,3,4,5]

arr.each do |item|
  puts item * 2
end

The code uses a basic loop in Ruby to iterate over an array called “arr” containing the numbers 1,2,3,4 and 5. The loop begins with the “each” keyword which will loop through each item in the array and assign it to the variable “item”. Between the “do … end” statement, the code says to take each “item” and “put” (output) it multiplied by 2. So the output of this code will be 2,4,6,8,10.

ruby