Iterators vs Loops
Loops in Ruby are used to execute the same block of code a specified number of times.
Ruby provides the different types of loop to handle the condition based situation in the program to make the programmers task simpler. The loops in Ruby are:
while loop
for loop
do..while loop
until loop
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.
There are many iterators in Ruby as follows:
Each Iterator
Collect Iterator
Times Iterator
Upto Iterator
Downto Iterator
Step Iterator
Each_Line Iterato
Loops are useful when we need to tell our program to do something a certain number of times or to do something based on a certain condition. Iterators are useful for operating on a collection of objects, and even performing complex operations on the members of that collection.
For example, if I tell my program to print out the phrase “I love programming!” five times, that’s looping. If I tell my program to enumerate over the array [1, 2, 3, 4, 5] and add 10 to each number, that’s iteration.
To read more about loops and iterators in ruby, check below link:
https://www.tutorialspoint.com/ruby/index.htm
Thanks for reading!