Ruby - Create a traditional type of for loop

Introduction

To create the traditional type of for loop, using a for loop to iterate over the values in a range.

For example, this is how to use a for loop variable to count up from 1 to 10, displaying its value at each turn through the loop:

Demo

for i in (1..10) do 
   puts( i ) 
end

Result

A range expression such as 1..3 must be enclosed between parentheses when used with the each method.

Related Topic