Ruby program to demonstrate the yield statement

Ruby Example: Write a program to demonstrate the yield statement.
Submitted by Nidhi, on December 28, 2021

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!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.