Ruby for Loop Tutorial

In this section, we will learn what the for loop is and how to use it in Ruby.

What is for loop in Ruby?

The Ruby for loop is used to running a set of instructions for a number of times.

Using for loop, we can also iterate through the elements of a collection like Arrays and Hashes and run a set of instructions for each element.

Ruby for loop syntax:

for variable_name in range/collection do

#The body of the for loop

end

– `for`: in order to create a for loop, we start with the keyword `for`.

– `variable_name`: as mentioned before, a for loop is capable of looping through the elements of a collection. So when the for loop starts to call a collection to get its elements, each of them will be passed to the `variable_name`. Note that every time an element of a collection is passed to the `variable_name`, the body of the for loop will run for that element.

– `range/collection`: Here we can put two data type:

  • Collection: this is a list of elements that we want to loop through its elements and run a set of instructions per each element.
  • Range: we can also create a set of numbers (for example 0 to 100) using something called range (which you’ll learn about it in a bit) and use that range to run the for loop for a number of times. For example, we can create a range between 0 to 10 if we want to run the for loop for 10 times. Or if we need to run the for loop for 50 times then we can use range and create a sequence of numbers from 0 to 50 which will cause the for loop to run for this number of times.

– `in`: between the `variable_name` and the `range/collection` we use the keyword `in` to separate these two elements of the for loop from each other.

– `do`: the `do` keyword defines the beginning the body of the for loop.

– `end`: the `end` keyword defines the closing body of the for loop.

Now between the `do` and `end` keyword, which is the body of the for loop, we can define whatever instructions that we want to be run per each iteration of the loop.

Example: using for loop in Ruby

colors = ["Blue", "Red", "Green", "Yellow", "White"]

for i in colors do

    puts i

end

Output:

Blue

Red

Green

Yellow

White

How Does for loop work in Ruby?

In the example above, the collection that we’ve iterated through its element was called `colors`. The variable that took each element of this list is called `i` and as you can see, the body of the for loop ran per each element in the list and we got the elements of the collection on the output stream.

Ruby Range using two dots operator

As mentioned before, the for loop can run a set of instructions within its body for a number of times! Now in order to define this sequence of number, we can use Ruby Range.

The range in Ruby has two forms.

Here’s the Range first form:

starting_number..ending_number

`starting_number`: this is the beginning of the sequence of the numbers. For example, if we want to create a range starting from the value 10, then we put the value 10 as the replacement of the `starting_number`.

`..`: the two dots that follow the `starting_number` is needed if we want to create a range. Basically, this is part of the syntax of a range.

`ending_number`: this is a number where it defines the last number in the sequence created by the range. Note that this value is inclusive. This means if we set the first value to 10 and the second value to 20, we will get a sequence of numbers starting from 10 and ending with the value 20. So in total we will have 11 numbers and so if we use such range in a for loop, the for loop will run for 11 times (11 times because both values are inclusive).

Example: using ranges in for loop

for i in 10..20 do

    puts "The value is: #{i}"

end

Output:

The value is: 10

The value is: 11

The value is: 12

The value is: 13

The value is: 14

The value is: 15

The value is: 16

The value is: 17

The value is: 18

The value is: 19

The value is: 20

As you can see, the body of the loop ran for 11 times, starting from the value 10 and ended with the value 20.

Ruby Range using three dots operator

Another form of the range operator is when we use 3 dots instead of two in the range.

In such case, the `ending_number` will be exclusive!

Example: Ruby for loop and ranges

for i in 10...20 do

    puts "The value is: #{i}"

end

Output:

The value is: 10

The value is: 11

The value is: 12

The value is: 13

The value is: 14

The value is: 15

The value is: 16

The value is: 17

The value is: 18

The value is: 19

As you can see, we’ve set the ending number to 20, but because we’ve used the 3 dots operator in the range, the last number excluded.

Ruby For Loop and Arrays

As mentioned before, using for loop, we can loop through the elements of a collection. This collection can be an Array, for example.

Note: if you’re not familiar with the arrays, you can check the Ruby Array section.

Example: iterating through the elements of an array using for loop

names = ["Jack","Omid","John","Elon","Ellen"]

for i in names do

    puts "The name is: #{i}"

end

Output:

The name is: Jack

The name is: Omid

The name is: John

The name is: Elon

The name is: Ellen

Here, the `names` variable represents an array. So we’ve used the for loop to iterate through its element and then printed them to the output stream.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies