Each Iterator
Each iterator is one of the most used types of iterators in ruby. Each iterator returns all the elements from an array or the hash. This iterator returns each value one by one.
Syntax
collection.each do |variable_name|
#body (code)
end

You can also try this code with Online Ruby Compiler
Run Code
Example
Let us see an example for each iterator.
Input
(1..5).each do | i |
puts i
end

You can also try this code with Online Ruby Compiler
Run Code
Output
1
2
3
4
5

You can also try this code with Online Ruby Compiler
Run Code
Times Iterator
In this iterator, a loop will execute a specified number of times. It starts from zero and goes to one less than the specified number.
Syntax
number.times do |variable_name|
#body (code)
end

You can also try this code with Online Ruby Compiler
Run Code
Here, number is the specified number that defines the number of iterations.
Example
Let us see an example for times iterator.
Input
5.times do | i |
puts i
end

You can also try this code with Online Ruby Compiler
Run Code
Output
1
2
3
4

You can also try this code with Online Ruby Compiler
Run Code
Collect Iterator
Collect iterator returns all the elements of the collection. It returns an entire collection.
Syntax
collection= collection.collect

You can also try this code with Online Ruby Compiler
Run Code
Example
Let us see an example for collector iterator.
Input
a=[1,2,3,4,5]
b=a.collect{ | i | (2*i)}
puts b

You can also try this code with Online Ruby Compiler
Run Code
Output
2
4
6
8
10

You can also try this code with Online Ruby Compiler
Run Code
Step Iterator
Step iterator is used to skip over a specific range of iterations.
Syntax
(controller).step(range) do |variable_name|
#body (code)
end

You can also try this code with Online Ruby Compiler
Run Code
Here, range refers to the range to be skipped during iteration.
Example
Let us see an example for step iterator.
Input
(10..50).step(5) do | i |
puts i
end

You can also try this code with Online Ruby Compiler
Run Code
Output
10
20
30
40
50

You can also try this code with Online Ruby Compiler
Run Code
Each_Line Iterator
Each_line iterator is used to iterate over a new line in a given string.
Syntax
string.each_line do |variable_name|
#body (code)
end
Example

You can also try this code with Online Ruby Compiler
Run Code
Example
Let us see an example for each_line iterator.
Input
“Types\nof\nIterators\nin\nRuby.”step(5) do | i |
puts i
end

You can also try this code with Online Ruby Compiler
Run Code
Output
Types
of
Iterators
in
Ruby

You can also try this code with Online Ruby Compiler
Run Code
Upto Iterator
Upto iterator follows the top to bottom approach. The iterator starts from the top and ends at the bottom. Also, the value of the bottom is always more significant (greater) than the top variable. If the value of the bottom is not greater than the top, it will return nothing.
Syntax
top.upto(bottom) do |variable_name|
#body (code)
end

You can also try this code with Online Ruby Compiler
Run Code
Example
Let us see an example for upto iterator.
Input
5.upto(10) do | i |
puts i
end

You can also try this code with Online Ruby Compiler
Run Code
Output
5
6
7
8
9
10

You can also try this code with Online Ruby Compiler
Run Code
Downto Iterator
Downto iterator is the opposite of Upto iterator. It follows the bottom-to-top approach. The iterator starts from the bottom and ends on the top. Also, the value of the bottom variable is always smaller than the top variable. If the value of the bottom variable is not smaller than the top, then it will return nothing.
Syntax
top.downto(bottom) do |variable_name|
#body (code)
end

You can also try this code with Online Ruby Compiler
Run Code
Example
Let us see an example for downto iterator.
Input
10.downto(5) do | i |
puts i
end

You can also try this code with Online Ruby Compiler
Run Code
Output
10
9
8
7
6
5

You can also try this code with Online Ruby Compiler
Run Code
We have learned about different types of iterators in Ruby with their syntax and an example. Now, let's see some frequently asked questions related to types of iterators in ruby.
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.
What is the difference between the loops and the iterators?
A loop is defined as a segment of code that executes multiple times, whereas Iterators refer to the methods commonly used to write loops.
What are Iterators in data structures?
An iterator is an object that can iterate over elements.
What are the different types of iterators in C++?
Following are the different types of iterators in C++-
- Input Iterator
- Output Iterator
- Forward Iterator
- Bidirectional Iterator
- Random Access Iterator
What is the main difference between the Upto and Downto Iterators in ruby?
In Upto Iterator, the value of the bottom variable is always greater than the top variable. In Downto iterator, the value of the bottom variable is always smaller than the top variable.
Conclusion
In this article, we have studied the types of iterators in Ruby with their syntax and an example.
After reading about the types of iterators in Ruby, are you not feeling excited to read/explore more articles on Ruby? Don't worry; Coding Ninjas has you covered. See Loops, History of Ruby, Try 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!
