Table of contents
1.
Introduction
2.
Block Structure
3.
The Yield Statement 
4.
Block and Method
5.
BEGIN and END Blocks
6.
Frequently Asked Questions
6.1.
What is a Block Structure in Ruby?
6.2.
How many different forms of syntax are there in a Ruby Block Structure?
6.3.
Explain Yield Statement?
6.4.
What is the BEGIN Block Structure in Ruby?
6.5.
What is the END Block Structure in Ruby?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Block Structure in Ruby

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we'll learn about Block Structure in Ruby and then the yield statement of Block Structure in Ruby. If you have some prior understanding of these Block Structures in Ruby, that is fantastic; if not, that is not an issue because we will be addressing Block Structure in Ruby and terminology from the ground up. But you surely need some hands-on Methods in Ruby before starting; if you don't know anything about Ruby Methods, don't worry. Coding Ninjas is always there for you.

Block Structure in Ruby

Block Structure

You've seen how Ruby constructs methods where you may write several statements and then call them. Ruby, too, has the idea of Block.

Other programming languages refer to Ruby code blocks as closures. It comprises a series of codes that are always separated by braces or written between do..end. The brackets syntax takes priority over the do..end syntax every time. Braces take priority overdo and vice versa.

  • A block is made up of code pieces.
  • A block is given a name.
  • The code in the Block is always surrounded by brackets ().
  • A block is always called from a function with the same name as the Block. This implies that if you have a block called test, you will use the function test to call it.
  • The yield statement is used to call a block.

 

As mentioned above there are two types of Syntax for Blocks 

  1. Syntax with the do..end statement
  2. Syntax with the curly braces {}

 

Syntax within the do..end statement:

block_name do 
   #statement-1  
   #statement-2  
end 
You can also try this code with Online Ruby Compiler
Run Code

 

Example:

[10, 20, 30].each do |n|   
 puts n   
end  
You can also try this code with Online Ruby Compiler
Run Code

 

Output:

10
20
30

 

Let’s see Blocks second type of syntax which is

Syntax within the curly braces {} :

block_name {
statement1
statement2
..........
}
You can also try this code with Online Ruby Compiler
Run Code

 

Example:

[10, 20, 30].each {|i| puts i} 
You can also try this code with Online Ruby Compiler
Run Code

 

Output:

10
20
30

 

Now, you will learn how to trigger a block using a simple yield statement in this section. You will also learn how to invoke a block using a yield statement with arguments. The example code will be tested with both forms of yield statements.

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

  1. Syntax within the do..end statement
  2. 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 OOPsTainting Objects in Ruby, and Object References in Ruby. You can also refer to the Official RubyRubyOfficial DocumentationRuby FAQ, and Ruby Koans.

You can also consider our Online Coding Courses such as the DSA in PythonC++ DSA CourseDSA in Java Course to give your career an edge over others.

Do upvote our blogs if you find them helpful and engaging!

Thank you

Live masterclass