ADVERTISEMENT
ADVERTISEMENT

Ruby program to create multiple BEGIN and END blocks

Ruby Example: Write a program to create multiple BEGIN and END blocks.
Submitted by Nidhi, on December 28, 2021

Problem Solution:

In this program, we will create multiple BEGIN and END blocks. The BEGIN blocks execute in sequential order, Whereas END blocks execute in reverse order.

Program/Source Code:

The source code to create multiple BEGIN and END blocks is given below. The given program is compiled and executed successfully.

# Ruby program to create multiple 
# BEGIN and END blocks

BEGIN { 
   puts "Statement in 1st BEGIN block";
} 
 
BEGIN { 
   puts "Statement in 2nd BEGIN block";
} 

END { 
    puts "Statement in 1st END block";
}
 
END { 
    puts "Statement in 2nd END block";
}

# Below statements will execute 
# before END block 
puts "Message1";
puts "Message2";

Output:

Statement in 1st BEGIN block
Statement in 2nd BEGIN block
Message1
Message2
Statement in 2nd END block
Statement in 1st END block

Explanation:

In the above program, we created multiple BEGIN and END blocks. Here all created BEGIN blocks are executed, then other statements are executed. After that, END blocks are executed in reverse order.

Ruby Blocks Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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.