Table of contents
1.
Introduction
2.
Blocks in Ruby
2.1.
Syntax of Ruby Blocks:
3.
Examples of Ruby Block
4.
Working of Yield Statement
5.
Frequently Asked Questions
5.1.
What are Objects in Ruby?
5.2.
What is a block in Ruby?
5.3.
What are the main types of blocks in Ruby?
5.4.
What is a Global Variable in Ruby?
5.5.
What are Global Functions in Ruby?
6.
Conclusion
Last Updated: Mar 27, 2024

Creating and Invoking a Block in Ruby

Author Rajat Agrawal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Ruby is a general-purpose, dynamic, reflective, object-oriented programming language. Everything in Ruby is an object. Ruby's development aimed to create a user interface between human programmers and the underlying computational machinery. 
As there are closures in different programming languages, Ruby have blocks. A block in Ruby is similar to a method but does not belong to an object.
Let’s learn about Blocks in Ruby in-depth.

Blocks in Ruby

In Ruby, a block is similar to a method. Ruby blocks enable us to execute any calculations and manipulation in the same manner as we would inside of any method. Therefore, we can say that while blocks in Ruby are identical to any method, they do not belong to any objects.

Properties of blocks:-

1.) Block can take arguments and return a value.

2.) Block does not have a unique name.

3.) Blocks are made of pieces of code.

4.) A block can be passed to a method call or is always called with a function.

5.) The yield statement is used to invoke a block within a method with a value.

6.) Blocks can be called from within the method they are passed to, just like methods can.


In Ruby, there are three different types of Blocks:-

1.) do and end statement blocks: The most typical way of creating any block in Ruby is this one. With the help of do, we may create repetition-based code and obtain each attribute.

2.) Inline blocks or with Curly braces: In Ruby, curly braces can be used to create any block, and inside of them, codes or codes for the compilation of certain task blocks can be written.

3.) Block with arguments: With the help of the || symbol, we may pass the block with certain arguments.

Syntax of Ruby Blocks:

The Ruby block syntax can be divided into three distinct sections.

1.) Inside the do…end statement Syntax:

block_name do 

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


2.) Inline blocks between the curly braces {} Syntax:

block_name { #statements_to_be_executed }
You can also try this code with Online Ruby Compiler
Run Code


3.) Block with arguments enclosing between the pipe or vertical bars || Syntax:

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

or

block_name {|arguments| #statements_to_be_executed }
You can also try this code with Online Ruby Compiler
Run Code

Examples of Ruby Block

Let’s see the working of Ruby blocks with the help of code examples.

Example 1:

Creating a block with do-end statements.

["DBMS", "OS", "CN","OOPS", "Algorithms", "Data Structures"].each do |subjects|
puts subjects
end
You can also try this code with Online Ruby Compiler
Run Code

Output:

Example 2:

Creating an Inline block or block using curly braces {}.

["DBMS", "OS", "CN","OOPS", "Algorithms", "Data Structures"].each {|subjects| puts subjects}
You can also try this code with Online Ruby Compiler
Run Code

Output:

Example 3:

Creating a block where we pass an argument to a Block.

subjects = ["DBMS", "OS", "CN","OOPS", "Algorithms", "Data Structures"]

subjects.each {|subjects| puts subjects}
You can also try this code with Online Ruby Compiler
Run Code

Output:

Example 4:

Creating a block where some logic is passed in the argument.

nums = [10,15,20,25,30,35,40,45,50]

puts nums.select { |number| number.even?}
You can also try this code with Online Ruby Compiler
Run Code

Output:

We have passed a logic on the list to select only even numbers from the list. We can see we are getting only even numbers from the list as our output.

Working of Yield Statement

A block inside a method is called using the yield keyword and a value in the yield statement.

Let’s see the working of the yield statement with the help of a code example.

# Ruby program to show the working yield statement

# method
def ninjas

# method statement to be executed
puts "Ninjas are Coders!"

# using yield statement
yield

# method statement to be executed
puts "Ninjas are Developers!"

# using yield statement
yield

end

# block
ninjas{puts "Ninjas are Data Scientists!"}
You can also try this code with Online Ruby Compiler
Run Code

Output:

In the above program, the method name is ninjas. Method statements are initially invoked, demonstrating that Ninjas are Coders!. However, as soon as yield statements are executed, control is transferred to the block, which then executes its own statements, demonstrating that Ninjas are Data Scientists!. When the Block completes, it returns control to the method, which then continues to run from where the yield statement was invoked. 

Frequently Asked Questions

What are Objects in Ruby?

In Ruby, everything is an object. All objects have a unique identification; they can also maintain a state and exhibit behavior in response to messages. Usually, these messages are sent out via method calls. A string is an example of a Ruby object.

What is a block in Ruby?

In Ruby, a block is similar to a method. Ruby blocks enable us to execute any calculations and manipulation in the same manner as we would inside of any method. Therefore, we can say that while blocks in Ruby are identical to any method, they do not belong to any objects.

What are the main types of blocks in Ruby?

There are mainly three types of blocks in Ruby:-

1.) do and end statement blocks.

2.) Inline blocks or with Curly braces.

3.) Block with arguments.

What is a Global Variable in Ruby?

The variable with global scope which can be accessed from anywhere in the program is known as Global Variable.

What are Global Functions in Ruby?

Kernel-specified methods and any methods defined at the top-level, outside of any classes, are Global Functions. Global functions are defined as private methods of the Object class.

Conclusion

In this article, we have extensively discussed Blocks in Ruby and how to create and invoke the blocks in Ruby with the help of code examples.

If you want to learn more, check out our articles on Object Marshalling in RubyTainting Objects in RubyCopying Objects In RubyHow To Invoke Global Functions In Ruby?, and Object References in Ruby.
Check out this problem - Redundant Braces

Do upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass