×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby program to demonstrate the yield statement

Last Updated : December 15, 2025

Problem Solution

In this program, we will demonstrate the yield statement. The yield statement is used to call block inside the method.

Program/Source Code

The source code to demonstrate the yield statement is given below. The given program is compiled and executed successfully.

# Ruby program to demonstrate the yield statement

def MyMethod
    puts "Inside the method"
    yield
      
    puts "Hello from MyMethod"
    yield
end
  
MyMethod{puts "Hello from Block"}

Output

Inside the method
Hello from Block
Hello from MyMethod
Hello from Block

Explanation

In the above program, we used the "yield" statement to call block inside the method and printed the appropriate message.

Ruby Blocks Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.