The For/in Loop in Ruby
The for/in loop iterates through the elements of an enumerable object (such as an array). Each iteration assigns an element to a specified loop variable and executes the loop's body.
for loop is preferred when we know the number of times loop statements are executed.
Let's see the syntax and some examples of for loop in ruby.
Syntax
Below is the syntax for the for loop in Ruby.
for variable_name expression(range, array variable) [d]
#body of the loop
end

You can also try this code with Online Ruby Compiler
Run Code
for- It is a keyword.
variable_name- It refers to the variable's name that refers to the current iteration of the loop.
expression- It refers to the execution of the code once for each element.
do- It is optional. It indicates the beginning of the block of code.
end- This is used to terminate the for loop or indicate the end of the loop.
Example
Let's see some examples of for loop in Ruby.
Example 1
Input:
a="Hello"
For i in a do
puts i
end

You can also try this code with Online Ruby Compiler
Run Code
Output:
Hello

You can also try this code with Online Ruby Compiler
Run Code
Example 2
Input:
arr=["For", "Loop", “in”, “Ruby”]
for i in a do
puts i
end

You can also try this code with Online Ruby Compiler
Run Code
Here, we declared an array arr.
Output:
For
Loop
in
Ruby

You can also try this code with Online Ruby Compiler
Run Code
Example 3
Input:
for i in 1..7 do
puts i
end

You can also try this code with Online Ruby Compiler
Run Code
Here, we defined the range, so we have used two-dot to include both 1 and 10. (two-dot operators are inclusive operators)
Output:
1
2
3
4
5
6
7

You can also try this code with Online Ruby Compiler
Run Code
We are done with the blog "for loop in ruby," so let's look at some Frequently Asked Questions related to them.
Frequently Asked Questions
What is Ruby?
Ruby is a programming language mainly used in web programming. Ruby is known among programmers for a terse, uncluttered syntax that doesn't require a lot of extra punctuation. Ruby is a modem language that makes it easy to use high-level abstractions such as metaprogramming.
Who invented the Ruby language?
Yukihiro "Matz" Matsumoto, a Japanese computer programmer, invented this language in the mid-1990s.
What is the difference between the while loop and for loop in Ruby?
Both have similar functionality but have different syntax. Also for loop is preferred when we know how many times the loop will be executed.
Why for loop in Ruby is also known as an entry-controlled loop?
for loop is also called an entry-controlled loop because the condition is present at the beginning of the loop body.
Why a do-while loop is also known as an exit control loop?
It is also known as an exit control loop because the condition to be tested is at the last of the loop body. Even if the condition is false, the loop executes at least once.
Conclusion
This article discusses the for loop in Ruby with their syntax and some examples.
After reading about the for loop in Ruby, are you not feeling excited to read/explore more articles on Data Structures and Algorithms? Don't worry; Coding Ninjas has you covered. See String literal in Ruby, History of Ruby, String Operators in Ruby, Constants in Ruby, and Hashes in Ruby to learn.
Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more!
Do upvote our blogs if you find them helpful and engaging!
Happy Learning!
