The Yield Statement
To write a method that takes blocks, you must first understand how the yield keyword works.
The yield statement uses the yield keyword with a value to call a block within the method.
Example:
def ninjas
# statement of the method to be executed
puts "This is the Method!"
# using the yield statement
yield
# statement of the method to be executed
puts "Again, back to the Method!"
# using the yield statement
yield
end
# block
ninjas{puts "This is Block!"}

You can also try this code with Online Ruby Compiler
Run Code
Output:
This is the Method!
This is Block!
Again back to the method!
This is Block!
Explanation: The name of the method in the preceding program is ninjas. First, method statements that display this method are called. However, as soon as the yield statements run, control passes to block, which executes its instructions. When the Block is executed, control is returned to the method, and the method continues to execute from where the yield statement was called.
You must be wondering that we are using Methods a lot, so what is the relation of Block and Method? Now let’s see the relation between Block and Method.
Block and Method
You've seen how a block and a method may be linked together. A block is generally invoked by using the yield statement from a method with the same name as the Block. As a result, you write
def test
yield
end
test{ puts "Hello ninja’s"}

You can also try this code with Online Ruby Compiler
Run Code
This is the most basic approach to implementing a block. The yield statement is used to invoke the test block.
However, if a method's last argument is preceded by &, you can give a block to this method, and this Block will be allocated to the last parameter. If both * and & appear in the argument list, & should come first.
def test(&block)
block.call
end
test { puts "Hello ninja’s"}

You can also try this code with Online Ruby Compiler
Run Code
Output:
Hello ninja’s
BEGIN and END Blocks
The BEGIN block in a Ruby source file is used to indicate the Block of code that will run once the file is loaded. After the program has finished running, the END block will be executed. A program can have several BEGIN and END blocks. BEGIN blocks always execute in the same order, whereas END blocks execute in reverse.
BEGIN {
# BEGIN block code
puts "BEGIN code block"
}
END {
# END block code
puts "END code block"
}
# MAIN block code
puts "MAIN code block"

You can also try this code with Online Ruby Compiler
Run Code
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. When run, the above software yields the following result:
BEGIN code block
MAIN code block
END code block
We are done with an explanation about block structure in Ruby now let’s see some Frequently asked questions related to this topic.
Frequently Asked Questions
What is a Block Structure in Ruby?
Ruby's blocks are one of the features that programmers adore. They are a strong feature that enables us to design incredibly flexible programs. At the same time, they read well and are widely utilized.
A block is roughly the same as a method, but it lacks a name and does not relate to an object.
How many different forms of syntax are there in a Ruby Block Structure?
There are two different types of Syntax for Blocks
- Syntax within the do..end statement
- Syntax within the curly braces {}
Explain Yield Statement?
The yield statement is only used when defining a generator function and only in the generator function's body. Using a yield statement in a function definition is enough to force that definition to generate a generator function rather than a standard function.
def test
What is the BEGIN Block Structure in Ruby?
BEGIN blocks (in uppercase) are reserved keywords in Ruby that are rather simple to employ. It allows us to identify code blocks that we want your program to run at the start of its execution, regardless of where they are in the source file.
What is the END Block Structure in Ruby?
Ruby's reserved keywords are END blocks (in uppercase). They let you define code blocks that you wish your program to execute at the conclusion of its execution, regardless of where they are in the source file.
Conclusion
In this article, we learned about Block Structure in Ruby, including types of Block Structure in Ruby and The yield statement with a sufficient number of examples.
After reading about Block Structure 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.
You can also consider our Online Coding Courses such as the DSA in Python, C++ DSA Course, DSA in Java Course to give your career an edge over others.
Do upvote our blogs if you find them helpful and engaging!
