Open In App

Ruby | Types of Iterators

Last Updated : 29 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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: 
 

  1. Each Iterator
  2. Collect Iterator
  3. Times Iterator
  4. Upto Iterator
  5. Downto Iterator
  6. Step Iterator
  7. Each_Line Iterator
    1. 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
  1. 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 array
a.each do|arr|
  
    # statement to be executed
    puts arr
     
end


  1. Output: 
     
0
1
2
3
4
5
6
7
8
9

G
e
e
k
s
  1.  
  2. 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
  1. 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 5
b = a.collect{ |y| (5 * y) }
puts b


  1. Output: 
     
5
10
15
20
25
30
35
40
45
50
  1. 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
  1. 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 value
7.times do |i|
    puts i
end


  1. Output: 
     
0
1
2
3
4
5
6
  1. 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
  1. 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 7
4.upto(7) do |n|  
  puts n  
end 
 
# here top > bottom
# so no output
7.upto(4) do |n|  
  puts n  
end 


  1. Output: 
     
4
5
6
7
  1.  
  2. 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
  1. 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 4
7.downto(4) do |n|  
  puts n  
end 
 
# here top < bottom
# so no output
4.downto(7) do |n|  
  puts n  
end 


  1. Output: 
     
7
6
5
4
  1.  
  2. 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 
  1. 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 i
end


  1. Output: 
     

0
10
20
30
40
50
60
  1.  
  2. 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
  1. 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 i
end


  1. Output: 
     
Welcome
to
GeeksForGeeks
Portal
  1.  


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads