Table of contents
1.
Introduction
2.
Types of Iterators in Ruby
3.
Each Iterator
3.1.
Syntax
3.2.
Example
4.
Times Iterator
4.1.
Syntax
4.2.
Example
5.
Collect Iterator
5.1.
Syntax
5.2.
Example
6.
Step Iterator
6.1.
Syntax
6.2.
Example
7.
Each_Line Iterator
7.1.
Syntax
7.2.
Example
8.
Upto Iterator
8.1.
Syntax
8.2.
Example
9.
Downto Iterator
9.1.
Syntax
9.2.
Example
10.
Frequently Asked Questions
10.1.
What is Ruby?
10.2.
What is the difference between the loops and the iterators?
10.3.
What are Iterators in data structures?
10.4.
What are the different types of iterators in C++?
10.5.
What is the main difference between the Upto and Downto Iterators in ruby?
11.
Conclusion
Last Updated: Mar 27, 2024
Medium

Types of Iterators in Ruby

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Iterators are a particular method that is commonly used to write loops (while, for, until, etc.) or is the method that is supported by collections (objects that store a group of data ). In Ruby, arrays and hashes are termed collections.

Iterators are the object-oriented concept and one of the most noteworthy features of Ruby.

This article will discuss the different types of iterators in ruby.

Types of Iterators in Ruby

Following are the different types of iterators in Ruby.

  1. Each Iterator
  2. Times Iterator
  3. Collect Iterator
  4. Step Iterator
  5. Each_Line Iterator
  6. Upto Iterator
  7. Downto Iterator

Types of Iterators in Ruby

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 LoopsHistory of RubyTry RubyConstants in Ruby, and Hashes in Ruby to learn.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! 

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Conclusion Image

Live masterclass