Ruby Tutorial Ruby References

Ruby - Iterators



Iterators is the object-oriented concept in Ruby. Iteration means doing one thing many times like a loop. Iterators are the methods which are supported by collections(Arrays, Hashes etc.) and returns all the elements from a collection, one after the other. Collections are the objects which store a group of data members.

Ruby supports a number of iterators which are mentioned below:

  • Ruby each iterator
  • Ruby times iterator
  • Ruby upto iterator
  • Ruby downto iterator
  • Ruby step iterator
  • Ruby each_line iterator
  • Ruby collect iterator

Ruby each iterator

The each iterator returns all elements of an array or a hash one by one.

Syntax

#method 1
collection.each do [|variable|]
  statements
end

#method 2
collection.each {[|variable|] statements}

In the example below, the each iterator is used to print all elements of an array.

MyNum = [10, 20, 30]
MyStr = ["Red", "Blue", "Green"]

#using each iterator to print
#all elements of MyNum
puts "MyNum contains:"
MyNum.each do |i|
  puts i
end

#using each iterator to print
#all elements of MyStr
puts "\nMyStr contains:"
MyStr.each {|i| puts i}

The output of the above code will be:

MyNum contains:
10
20
30

MyStr contains:
Red
Blue
Green

Ruby collect iterator

The collect iterator returns all the elements of a collection, regardless of whether it is an array or hash.

Syntax

#method 1
collection.collect do [|variable|]
  statements
end

#method 2
collection.collect {[|variable|]
  statements
}

The example below shows the usage of collect iterator.

MyNum = [10, 20, 30]
MyStr = ["Red", "Blue", "Green"]

#using collect iterator to print
#all elements of MyNum
puts "MyNum contains:"
MyNum.collect do |i|
  puts i
end

#using collect iterator to create 
#new array from MyNum
puts "\nNewNum contains:"
NewNum = MyNum.collect {|i| (i + 5)}
puts NewNum

#using collect iterator to print
#all elements of MyStr
puts "\nMyStr contains:"
MyStr.collect {|i| puts i}

The output of the above code will be:

MyNum contains:
10
20
30

NewNum contains:
15
25
35

MyStr contains:
Red
Blue
Green

Ruby times iterator

The times iterator enables to execute the loop by specified number of times. The loop is initially started from zero and runs until the one less than the specified number.

The times iterator can be used with no iteration variable. The iteration variable can be added by using the vertical bars around the identifier.

Syntax

#method 1
t.times do [|variable|]
  statements
end

#method 2
t.times {[|variable|]
  statements
}

The example below shows the usage of times iterator.

#using times iterator to print "Hello"
#by given number of times
5.times do
  puts "Hello"
end

puts
#using times iterator to print iteration variable
5.times do |i|
  puts "i = #{i}"
end


The output of the above code will be:

Hello
Hello
Hello
Hello
Hello

i = 0
i = 1
i = 2
i = 3
i = 4

Ruby upto iterator

The upto iterator follows top to bottom approach. The loop is initially started from top and ends on bottom.

The upto iterator can be used with no iteration variable. The iteration variable can be added by using the vertical bars around the identifier.

Syntax

#method 1
top.upto(bottom) do [|variable|]
  statements
end

#method 2
top.upto(bottom) {[|variable|]
  statements
}

The example below shows the usage of upto iterator.

#using upto iterator to print "Hello"
#by given number of upto
5.upto(10) do
  puts "Hello"
end

puts
#using upto iterator to print iteration variable
5.upto(10) do |i|
  puts "i = #{i}"
end


The output of the above code will be:

Hello
Hello
Hello
Hello
Hello
Hello

i = 5
i = 6
i = 7
i = 8
i = 9
i = 10

Ruby downto iterator

The downto iterator follows bottom to top approach. The loop is initially started from bottom and ends on top.

The downto iterator can be used with no iteration variable. The iteration variable can be added by using the vertical bars around the identifier.

Syntax

#method 1
bottom.downto(top) do [|variable|]
  statements
end

#method 2
bottom.downto(top) {[|variable|]
  statements
}

The example below shows the usage of downto iterator.

#using downto iterator to print "Hello"
#by given number of downto
10.downto(5) do
  puts "Hello"
end

puts
#using downto iterator to print iteration variable
10.downto(5) do |i|
  puts "i = #{i}"
end


The output of the above code will be:

Hello
Hello
Hello
Hello
Hello
Hello

i = 10
i = 9
i = 8
i = 7
i = 6
i = 5

Ruby step iterator

The step iterator is used to iterate where the user requires a step size in the iteration.

Syntax

#method 1
collection.step(step_size) do [|variable|]
  statements
end

#method 2
collection.step(step_size) {[|variable|]
  statements
}

The example below shows the usage of step iterator.

#using step iterator to print all numbers
#from 5 to 50 with step size of 5
(5..50).step(5) do |i|
  puts i
end

The output of the above code will be:

5
10
15
20
25
30
35
40
45
50

Ruby each_line iterator

The each_line iterator is used to iterate over a new line in the given string.

Syntax

#method 1
string.each_line do [|variable|]
  statements
end

#method 2
string.each_line {[|variable|]
  statements
}

The example below shows the usage of each_line iterator.

MyStr = "Programming\nis\nfun."

#using each_line iterator to print
#given string
MyStr.each_line do |i|
  puts i
end

The output of the above code will be:

Programming
is
fun.

5