Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




RUBY - ITERATORS


So far we have seen, how to Iterate through an Array or Hash using a for loop.


Example :



the_array = [1, 3, 5, 8]
for i in the_array
	puts i
end


Output :



  1
  3
  5
  8

Now, there is another way by which we can Iterate an Array or Hash. And that is an Iterator.


An Iterator is a Ruby object that is used to Iterate an Array or Hash.


To understand Iterator, let us rewrite the above example with each Iterator.


The each Iterator


Example :



the_array = [1, 3, 5, 8]
the_array.each do |i|
	puts i
end	


Output :



  1
  3
  5
  8

So, in the above code, what we have done is, created a list named, the_list.


the_array = [1, 3, 5, 8]

And used the each statement to iterate the Array.


the_array.each do |i|
	puts i
end
java_Collections


The first task would be, to get the values of the array, the_array.


So, in the first iteration, the first value of the Array (i.e. 1) is taken to the variable i. And the print statement,


puts i

Prints the first element of the Array the_array.


1

And in the same way the other elements of the Array are printed.


The times Iterator


Say, we want to Iterate a loop and print the numbers from 0 to 9. A simplest solution could be the times Iterator.


Example :



x = 10
x.times do |i|
	puts i
end	


Output :



  0
  1
  2
  3
  4
  5
  6
  7
  8
  9

So, in the above code, what we have done is, printed the values from 0 to 9.


And we have achieved using times Iterator.


x.times do |i|
	puts i
end

The initial value of i is automatically set to 0 at the beginning of the loop. And at each Iteration, the value of i gets incremented by 1.


And prints the value of i at each Iteration.


The Upto Iterator


Say, we want to Iterate a loop and print the numbers from 5 to 9. Again that can be achieved using the upto Iterator.


Example :



x = 5
y = 9
x.upto(y) do |i|
	puts i
end	


Output :



  5
  6
  7
  8
  9

So, in the above code, what we have done is, printed the values from 5 to 9.


And we have achieved using upto Iterator.


x.upto(y) do |i|
	puts i
end
java_Collections


The initial value of i is automatically set to the value of x(i.e. 5). And the ending position is set to y(i.e. 9).


And at each Iteration the value of i gets incremented by 1, printing the value of i at each Iteration.


The Downto Iterator


The Downto Iterator is just the opposite of Upto Iterator.


Say, we want to Iterate a loop and print the numbers from 9 to 5. Again that can be achieved using the downto Iterator.


Example :



x = 9
y = 5
x.downto(y) do |i|
	puts i
end	


Output :



  9
  8
  7
  6
  5

So, in the above code, what we have done is, printed the values from 9 to 5.


And we have achieved using downto Iterator.


x.downto(y) do |i|
	puts i
end
java_Collections


The initial value of i is automatically set to the value of x(i.e. 9). And the ending position is set to y(i.e. 5).


And at each Iteration the value of i gets decremented by 1, printing the value of i at each Iteration.


The Step Iterator


Say, we want to Iterate a loop and print the even numbers between 0 to 10. So, exactly 2 has to added to each number at each number.


0, 2, 4, 6, 8, 10

This can be achieved using the step Iterator.


Example :



x = 2
(0..10).step(y) do |i|
	puts i
end	


Output :



  0
  2
  4
  6
  8
  10

So, in the above code, what we have done is, printed the numbers,


0, 2, 4, 6, 8, 10

And we have achieved using step Iterator.


(0..10).step(y) do |i|
	puts i
end
java_Collections


The initial value of i is automatically set to the value of the range i.e. (0..10)


And at each Iteration the value of i gets incremented by x(i.e. 2), printing the value of i at each Iteration.


The Each_Line Iterator


Say, we have the below paragraph,

In a huge pond,

there lived many fish.

They were arrogant and

never listened to anyone.

And we want each line to be printed at each Iteration.


This can be achieved using the each_line Iterator.


Example :



x = "In a huge pond, 
there lived many fish.
They were arrogant and 
never listened to anyone."

x.each_line do |i|   
	puts i   
end  


Output :



  In a huge pond,
  there lived many fish.
  They were arrogant and
  never listened to anyone.

The Collect Iterator


Say, we have an Array.


a = [1,2,3,4,5]

And we want to multiply each element with 5 and display the result.


i.e.


1 * 5
2 * 5
3 * 5
4 * 5
5 * 5

This can be done in a single line using collect Iterator.


Example :



a = [1,2,3,4,5]
b = a.collect{|i| i*5}
puts b


Output :



  5
  10
  15
  20
  25
java_Collections