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 Iterator
- Collect Iterator
- Times Iterator
- Upto Iterator
- Downto Iterator
- Step Iterator
- Each_Line Iterator
- 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:
Ruby
( 0 .. 9 ). each do |i|
puts i
end
a = [ 'G' , 'e' , 'e' , 'k' , 's' ]
puts "\n"
a. each do |arr|
puts arr
end
|
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:
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:
Ruby
a = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
b = a.collect{ |y| ( 5 * y) }
puts b
|
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:
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:
Ruby
7 .times do |i|
puts i
end
|
- 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:
Ruby
4 .upto( 7 ) do |n|
puts n
end
7 .upto( 4 ) do |n|
puts n
end
|
-
- 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:
Ruby
7 .downto( 4 ) do |n|
puts n
end
4 .downto( 7 ) do |n|
puts n
end
|
-
- 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:
Ruby
( 0 .. 60 ).step( 10 ) do |i|
puts i
end
|
-
- 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
Ruby
"Welcome\nto\nw3wiki\nPortal" .each_line do |i|
puts i
end
|