BEGIN, END in Ruby

2020-08-24 hit count image

Let's see what BEGIN and END are, and how to use them in Ruby.

Outline

In this blog post, we will see what BEGIN and END are, and how to use them. Simply, the BEGIN block is executed before the program is started, and the END block is executed after the program is finished.

BEGIN block

When you define something in the BEGIN block, the block is executed before all definitions.

puts "Hello"
BEGIN { puts "World" }
# World
# Hello

If there are multiple BEGIN blocks, the blocks are executed in order.

puts "Hello"
BEGIN { puts "World" }
BEGIN { puts "BEGIN" }
# World
# BEGIN
# Hello

The BEGIN block has a local variable scope, so you can’t share the other local variables.

BEGIN {
  $a = 0
  b = 0
}
puts $a # Global Variable
puts b # Local Variable

# 0
# undefined local variable or method 'b' for main:Object (NameError)

END block

The END block is executed after all codes are executed.

END { puts 5 }
puts 1
# 1
# 5

If there are multiple END blocks, the blocks are executed in the reverse order.

END { puts 5 }
END { puts 3 }
puts 1
# 1
# 3
# 5

Unlike the BEGIN block, the END block shares the variable scope. So you can share the local variable with the END block.

i = 1
END { puts i }
# 1

Completed

The BEGIN/END blocks are not often used, but it’s Ruby’s unique feature, so I think it has valuable for remembering.

Posts