×

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 BEGIN and END blocks

Last Updated : December 15, 2025

Problem Solution

In this program, we will demonstrate the BEGIN and END blocks. The BEGIN block executes at the start of the program and END block executes at the end of the program, and other statements execute before the END block.

Program/Source Code

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

# Ruby program to demonstrate the 
# BEGIN and END blocks

BEGIN { 
   puts "Statement in BEGIN block";
} 
  

END { 
    puts "Statement in END block";
}
  
# Below statements will execute 
# before END block 
puts "Message1";
puts "Message2";

Output

Statement in BEGIN block
Message1
Message2
Statement in END block

Explanation

In the above program, we created BEGIN and END blocks. The BEGIN block calls at the beginning of program execution and END block calls at the end of program execution, and other statements executes before the execution of the end block.

Ruby Blocks Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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