Ruby program to create multiple BEGIN and END blocks

bookmark

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