The yield statement
The yield statement is used to execute a method block with a value.
Let's break it down with an example to help with understanding.
Def met
puts, "This is Ruby."
Yield
puts, “We will be back to Ruby.”
yield
end
met {puts “This is Block”}

You can also try this code with Online Ruby Compiler
Run Code
Output:
This is Ruby
This is Block
We will be back at Ruby
This is Block
In this example, when we reach the yield line during the execution of the met method, the code inside the Block is performed. When the block execution is complete, the code for the met method resumes.
Passing Parameters with a yield statement
Let's understand it with the help of an Example:-
def test
yield 5
puts "We are in Ruby test."
yield 10
end
test {|i| puts "We are in the block #{i}"}

You can also try this code with Online Ruby Compiler
Run Code
Output:
We are in the block 5
We are in the Ruby test
We are in the Block 10
In this example, the yield statement is written first, followed by parameters. You may even pass several parameters. To accept the arguments, you add a variable between two vertical lines (||) in the Block. As a result, the yield 5 statement in the preceding code passes the number 5 as a parameter to the text block.
Consider the following statement:
test {|i| puts "We are in the block #{i}"}
In this case, the variable receives the value 5. Consider the following puts statement:
puts "We are in block #{i}."
This puts command produces the output.
You are in the block 5
If you wish to pass many parameters, the yield statement becomes
yield a, b
and the Block is −
test {|a, b| statement}
Commas will be used to separate the parameters.
As we are done with the Yield statement, now let's move on to Block variables.
Block Variables
We can use the same variable both within and outside of a block parameter. Consider the following example.
x= “Outer variable”
3.times do |x|
puts “Inside block: #{x}”
end
puts “Outside block: #{x}”

You can also try this code with Online Ruby Compiler
Run Code
Output:
Inside Block: 0
Inside Block: 1
Inside Block: 2
Outside Block: Outer variable
In this example, we use the same variable both within and outside the Block as block argument x.
BEGIN and END Blocks
Every Ruby source file can include code blocks that will be executed both when the file is loaded (the BEGIN blocks) and after the program has completed running (the END blocks).
A program may have several BEGIN and END blocks. BEGIN blocks are processed in the order in which they are encountered. END blocks are performed backward.
We've already covered Blocks, so let's move on to Iterators, which will cover the second part of this blog.
Ruby Iterators
Iterator is a term used in object-oriented programming. Iteration is the repetition of a single action, similar to a loop.
The loop method is the most basic Iterator. They return all the components of a collection one by one. Arrays and hashes are examples of collections.
There are mainly four types of Iterators which are Each, Upto & Downto, Step, and Time Iterators. Let's define each one by one.
Ruby Each Iterator
Each Iterator in Ruby returns all the items of a hash or array.
Syntax:
Collection.each do |variable|
Code…
end

You can also try this code with Online Ruby Compiler
Run Code
Code is executed for each element in the collection. The collection might be an array or a Ruby hash in this case.
Let's take an example for better understanding: -
ary = [1,2,3,4,5]
ary.each do |i|
puts i
End

You can also try this code with Online Ruby Compiler
Run Code
Output:
1
2
3
4
5
Each Iterator is always associated with a block. It returns each array value to the Block one by one. The value is saved in the variable I before being displayed on the screen.
We defined Each Iterator till now, and let's move on to the second type, which is the time Iterator.
Ruby Time Iterators
The time iterator executes a loop a specified number of times. The loop will begin at zero and continue until one less than the provided value is reached.
Syntax:
x.times do |variable|
Code…
end

You can also try this code with Online Ruby Compiler
Run Code
To iterate the loop, we must define a number in place of x.
Ruby Upto and Downto Iterators
An upto iterator loops over the numbers x and y.
Syntax:
x.upto(y) do |variable|
code
end

You can also try this code with Online Ruby Compiler
Run Code
Ruby Step Iterators
A step iterator is used to iterate across a range while skipping over it.
Syntax:
(controller).step(x) do |variable|
code
end

You can also try this code with Online Ruby Compiler
Run Code
In this case, x represents the range that will be skipped during iteration.
We recently finished Blocks and Iterators in Ruby, and I hope you understand it well. Let's see whether you recall what we've covered so far.
Check out this problem - Redundant Braces
Frequently Asked Questions
What exactly are Ruby blocks?
Other programming languages refer to Ruby code blocks as closures. It is made up of a series of codes that are always separated by braces or written between do...end.
How many different ways a Ruby block may be written?
There are two methods to write a block are several lines between doing and end and Inline between the braces
Both are identical and serve the same purpose.
In Ruby, what is the yield statement?
The yield statement is used to invoke a method block with a value.
What exactly are Ruby iterators?
Iterator is a term used in object-oriented programming. Iteration is the repetition of a single action, similar to a loop.
The loop method is the most basic Iterator. They return all the components of a collection one by one. Arrays and hashes are examples of collections.
In Ruby, how many iterators are there?
The iterators in Ruby are each Iterator, times Iterator, upto and downto Iterator, step iterator, and each_line Iterator.
Conclusion
In this article, we learned about Blocks and Iterators in Ruby, including Ruby Blocks Variables, BEGIN and END Blocks, Each Iterator, and Ruby Time Iterators. Blocks and Iterators in Ruby are significantly important topics for Interviewers. I hope you all understand about Blocks and Iterators in Ruby. After reading about Blocks and Iterators in Ruby, are you not feeling excited to read/explore more articles on the topic of Ruby? Don't worry; Coding Ninjas has you covered. To learn, see Ruby OOPs, Tainting Objects in Ruby, and Object References in Ruby. You can also refer to the Official Ruby, Ruby, Official Documentation, Ruby FAQ, and Ruby Koans.
Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Do upvote our blogs if you find them helpful and engaging!
