Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In our day-to-day life, we make a lot of decisions. Our life is not simple as a program executing sequentially. A sequential program is one where lines of code are executed one after the other without branching or repetition. It’s simple, but any program could rarely be that simpler.
Like in our lives, we make a decision based on a particular condition. We have to identify a way in ruby of telling the computer to execute some code conditionally: to execute it only if some condition is satisfied/ or execute it until the time condition is satisfied. One such way is an iterator in ruby. We will understand all about an iterator in ruby in this article but before that, have a brief introduction to statement and control structures.
Statement and Control Structures
So as we already explained, having a simple program that consists primarily of method invocations and variable assignment is rare. What makes such a program particularly simple is its purely sequential execution. Program is executed one after the other without branching or repetition. It is rare seeing any program for being that simple. An Iterator in ruby tells the computer to do one thing multiple times until some condition is satisfied and follows a control structure.
PROGRAM CONTROL STRUCTURE
Iterators and Enumerable Objects
Although loops constitute a significant part of the Ruby language for defining program control structure, it is probably more common to write loops rather than using unique methods called iterators. Iterators in ruby are one of the most noteworthy features of Ruby and help us in defining the control structure of ruby. We use the term iterator to mean any yield statement method. They do not serve the purpose of an iteration or looping function. For example, data structures like Array, Hash, Range, and several other classes define each iterator that passes each collection element to the associated block. Iterator is a concept of Object-Oriented Programming Language and, in simple terms, often defined as methods supported by collections such as an array, hashes etc. Iterator in ruby returns all the laments one after the other.
Similarly, an Enumerable object is one whose purpose is to enumerate over some other object. Now that you got a brief understanding of iterators and enumerable objects in ruby, let's get to the main discussion. The iterator in ruby. Now down the section, we will be getting to know each and every iterator in ruby in detail.
Types of Iterators in Ruby
There are many iterators in ruby, and we will discuss each one in detail with code implementation down the section.
Each Iterator in Ruby
Each iterator in ruby returns all the elements of a collection. The collection here could be an array or a hash, or it could also be a range. Each iterator returns each value of an array or hash one by one.
Each iterator in ruby follows a fundamental syntax as described below.
Collection_name.each do | Variable name|
# Code we want t run iteratively
End
You can also try this code with Online Ruby Compiler
Below is the implementation of each iterator in ruby
CODE
# Ruby Program to illustrate the implementation of each iterator in ruby
# using each iterator with the collection as a range
(1..5).each do |var|
# Code to iterate
puts var
end
ans_array = ['N','i','n','j','a']
puts "\n"
# using each iterator with the collection as an array
ans_array.each do|var|
# statement to be executed
puts var
end
You can also try this code with Online Ruby Compiler
Collect iterator in ruby returns all the elements present in a collection. The collect iterator in ruby would return an entire collection, no matter if it's an array or hash.
Syntax
Collection_name = collection.collect
You can also try this code with Online Ruby Compiler
Times iterator in ruby helps us to implement a loop a specific number of times. The loop would initially start from zero and runs until it reaches a number that is one less than the specified number.
Times Iterator in ruby could even be used without any iteration variable. However, we could add an iteration variable using the vertical bars around the identifier.
CODE
# Times iterator in ruby implementation
# Using times iterator in ruby by providing five as the iterated value
5. times do |var|
puts var
end
You can also try this code with Online Ruby Compiler
Upto iterator in ruby follows top to bottom approach. It includes both the top and bottom variables in the iteration. In Upto, iteration starts from the tops and ends after reaching the bottom. However, a crucial thing to notice here is that the bottom variable indeed to be always greater than the top variable. If it is not greater, then it will return nothing.
CODE
# Upto iterator in ruby implementation
# Using Upto iterator in ruby with top value is 10 and bottom value is 5
5.upto(10) do |var|
puts var
end
You can also try this code with Online Ruby Compiler
Downto iterator in ruby follows bottom to top approach. It includes both the top and bottom variables in the iteration. In Downto, iteration starts from the bottoms and ends after reaching the top. However, a crucial thing to notice here is that the bottom variable indeed to be always smaller than the top variable. If it is not smaller, then it will return nothing.
CODE
# Downto iterator in ruby implementation
# Using downto iterator i ruby with top value is 10 and bottom value is 5
10.downto(5) do |var|
puts var
end
You can also try this code with Online Ruby Compiler
Primary Difference Between an Iterator in Ruby and a Loop
The difference between a loop or an iterator in ruby is quite subtle. But both loops and iterators are generally used to repeat a code section. Iterator in ruby is a relatively new idea and exists only for a few languages; however, Loops are an old idea. We could even find the existence of a loop long before computers existed—almost every programming language supports a loop.
So is it the only difference? No Loops are powerful and are pretty dangerous. If unable to satisfy a termination condition, it could easily break our program by behaving as an infinite loop. On the other hand, an infinite loop is impossible if we are using an iterator instead:
Also, Iterators allow us to write more expressive, clutter-free code. Because they abstract away from the nitty-gritty details of a loop. Compare by yourself:
# loop vs iterator
CODE (LOOPS)
i = 0
while i < 5
puts "Hello"
i += 1
End
You can also try this code with Online Ruby Compiler
Ruby is an open-source dynamic language with natural and easy-to-read/write syntax. Ruby is a careful balance language and a perfect blend of five different languages.
What are enumerators in ruby?
An enumerator in ruby is a class that allows iterations of both types – external and internal. Internal iteration is when the class in question controls iteration, while external iteration is when the environment or the client controls iteration.
Mention Some of the iterators in Ruby
Iterators in ruby help us in defining the control structure of the program. Each, Times, Upto, Downto, Step, Each_Line are iterators in ruby.
What is an iterator in ruby?
Iterators in ruby are one of the most noteworthy features of Ruby and help us in defining the control structure of ruby. Iterator in ruby passes each collection element to the associated block.
Is Ruby Case Sensitive?
Ruby is a case-sensitive language, which means lowercase identifiers in ruby are different from uppercase identifiers. E.g. Like_This is entirely different from LIKE_THIS. Similarly, Start is different from START.
Conclusion
In this article, we have studied iterators in Ruby in detail and briefly explored all the iterators in ruby along with the code implementation. We expect that this article must have helped you enhance your knowledge on the topic of Ruby.