Ruby | Types of Iterators

29 May, 2021
The word iterate means doing one thing multiple times and that is what iterators do. Sometimes iterators are termed as the custom loops. 
“Iterators” is the object-oriented concept in Ruby.
In more simple words, iterators are the methods which are supported by collections(Arrays, Hashes etc.). Collections are the objects which store a group of data members.
Ruby iterators return all the elements of a collection one after another.
Ruby iterators are “chainable” i.e adding functionality on top of each other.
There are many iterators in Ruby as follows:  
Each IteratorCollect IteratorTimes IteratorUpto IteratorDownto IteratorStep IteratorEach_Line IteratorEach Iterator: This iterator returns all the elements of an array or a hash. Each iterator returns each value one by one. Syntax:  
Each Iterator
Collect Iterator
Times Iterator
Upto Iterator
Downto Iterator
Step Iterator
Each_Line IteratorEach Iterator: This iterator returns all the elements of an array or a hash. Each iterator returns each value one by one. Syntax:  
Each Iterator: This iterator returns all the elements of an array or a hash. Each iterator returns each value one by one. Syntax:  
Each Iterator: This iterator returns all the elements of an array or a hash. Each iterator returns each value one by one. Syntax:  
collection.each do |variable_name|
   # code to be iterate
end
In the above syntax, the collection can be the range, array or hash. Example:  
In the above syntax, the collection can be the range, array or hash. Example:  
Ruby
# Ruby program to illustrate each iterator #!/usr/bin/ruby    # using each iterator# here collection is range# variable name is i(0..9).each do |i|         # statement to be executed    puts i     end a = ['G', 'e', 'e', 'k', 's'] puts "\n" # using each iterator# here collection is an arraya.each do|arr|      # statement to be executed    puts arr     end
Output:  
Output:  
0
1
2
3
4
5
6
7
8
9

G
e
e
k
s
 Collect Iterator: This iterator returns all the elements of a collection. The collect iterator returns an entire collection, regardless of whether it is an array or hash.Syntax:  
 
Collect Iterator: This iterator returns all the elements of a collection. The collect iterator returns an entire collection, regardless of whether it is an array or hash.Syntax:  
Collection = collection.collect
The collect method need not always be associated with a block. The collect method returns the entire collection, regardless of whether it is an array or a hash.Example:  
The collect method need not always be associated with a block. The collect method returns the entire collection, regardless of whether it is an array or a hash.Example:  
Ruby
# Ruby program to illustrate the collect iterator #!/usr/bin/ruby a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # using collect iterator# printing table of 5b = a.collect{ |y| (5 * y) }puts b
Output:  
Output:  
5
10
15
20
25
30
35
40
45
50
Times Iterator: In this iterator, a loop is implanted with the specific number of time. The loop is initially started from zero and runs until the one less than the specified number.This can be used with no iteration variable.. We can add an iteration variable by using the vertical bars around the identifier. Syntax: 
Times Iterator: In this iterator, a loop is implanted with the specific number of time. The loop is initially started from zero and runs until the one less than the specified number.This can be used with no iteration variable.. We can add an iteration variable by using the vertical bars around the identifier. Syntax: 
t.times do |variable_name|

# code to be execute

end
Here t is the specified number which is used to define the number of iteration.Example: 
Here t is the specified number which is used to define the number of iteration.Example: 
Ruby
# Ruby program to illustrate time iterator #!/usr/bin/ruby # using times iterator by providing# 7 as the iterate value7.times do |i|    puts iend
Output:  
Output:  
0
1
2
3
4
5
6
Upto Iterator: This iterator follows top to bottom approach. It includes both the top and bottom variable in the iteration. Syntax:  
Upto Iterator: This iterator follows top to bottom approach. It includes both the top and bottom variable in the iteration. Syntax:  
top.upto(bottom) do |variable_name|

# code to execute

end
Here iteration starts from top and ends on bottom. The important point to remember that the value of bottom variable is always greater than the top variable and if it is not, then it will return nothing.Example:  
Here iteration starts from top and ends on bottom. The important point to remember that the value of bottom variable is always greater than the top variable and if it is not, then it will return nothing.Example:  
Ruby
# Ruby program to illustrate the upto iterator #!/usr/bin/ruby # using upto iterator# here top value is 4# bottom value is 74.upto(7) do |n|    puts n  end  # here top > bottom# so no output7.upto(4) do |n|    puts n  end 
Output:  
Output:  
4
5
6
7
 Downto Iterator: This iterator follows bottom to top approach. It includes both the top and bottom variable in the iteration. Syntax:  
 
Downto Iterator: This iterator follows bottom to top approach. It includes both the top and bottom variable in the iteration. Syntax:  
top.downto(bottom) do |variable_name|

# code to execute

end
Here iteration starts from bottom and ends on top. The important point to remember that the value of bottom variable is always smaller than the top variable and if it is not, then it will return nothing.Example:  
Here iteration starts from bottom and ends on top. The important point to remember that the value of bottom variable is always smaller than the top variable and if it is not, then it will return nothing.Example:  
Ruby
# Ruby program to illustrate the downto iterator #!/usr/bin/ruby # using downto iterator# here top value is 7# bottom value is 47.downto(4) do |n|    puts n  end  # here top < bottom# so no output4.downto(7) do |n|    puts n  end 
Output:  
Output:  
7
6
5
4
 Step Iterator: Ruby step iterator is used to iterate where the user has to skip a specified range. Syntax:  
 
Step Iterator: Ruby step iterator is used to iterate where the user has to skip a specified range. Syntax:  
Collection.step(rng) do |variable_name|

# code to be executed

end 
Here rng is the range which will be skipped throughout the iterate operation. Example: 
Here rng is the range which will be skipped throughout the iterate operation. Example: 
Ruby
# Ruby program to illustrate step iterator #!/usr/bin/ruby # using step iterator# skipping value is 10# (0..60 ) is the range(0..60).step(10) do|i|    puts iend
Output:  
Output:  
Output:  
0
10
20
30
40
50
60
 Each_line Iterator: Ruby each_line iterator is used to iterate over a new line in the string. Syntax: 
 
Each_line Iterator: Ruby each_line iterator is used to iterate over a new line in the string. Syntax: 
string.each_line do |variable_name|

# code to be executed

end
Example: 
Example: 
Ruby
# Ruby program to illustrate Each_line iterator #!/usr/bin/ruby  # using each_line iterator"Welcome\nto\nGeeksForGeeks\nPortal".each_line do|i|puts iend
Output:  
Output:  
Welcome
to
GeeksForGeeks
Portal
 
 
Code_Mech
Ruby
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Make a Custom Array of Hashes in Ruby?
Ruby | Enumerator each_with_index function
Ruby | unless Statement and unless Modifier
Ruby | Array class find_index() operation
Ruby For Beginners
Ruby | String concat Method
Ruby | Array shift() function
Ruby on Rails Introduction
Ruby | Types of Variables