BEGIN and END

BEGIN and ENDare reserved words in Ruby that declare code to be executed at the very beginning and very end of a Ruby program. (Note that BEGIN and END in capital letters are completely different from begin and end in lowercase.) If there is more than one BEGIN statement in a program, they are executed in the order in which the interpreter encounters them. If there is more than one END statement, they are executed in the reverse of the order in which they are encounteredā€”that is, the first one is executed last. These statements are not commonly used in Ruby. They are inherited from Perl, which in turn inherited them from the awk text-processing language.

BEGIN and END must be followed by an open curly brace, any amount of Ruby code, and a close curly brace. The curly braces are required; do and end are not allowed here. For example:

BEGIN {
  # Global initialization code goes here
}

END {
  # Global shutdown code goes here
}

The BEGIN and END statements are different from each other in subtle ways. BEGIN statements are executed before anything else, including any surrounding code. This means that they define a local variable scope that is completely separate from the surrounding code. It only really makes sense to put BEGIN statements in top-level code; a BEGIN within a conditional or loop will be executed without regard for the conditions that surround it. Consider this code:

if (false) 
  BEGIN {
    puts "if";                   # This will be printed
    a = 4;                       # This variable only defined here
  }
else
  BEGIN { puts "else" }          # Also printed
end

10.times {BEGIN { puts "loop" }} # Only printed once

The code associated with all three BEGIN statements will be executed once, and only once, regardless of the context in which it appears. Variables defined within BEGIN blocks will not be visible outside the block, and no variables outside the block will have been defined yet.

END statements are different. They are executed during normal program execution, so they share local variables with the surrounding code. If an END statement is within a conditional that is not executed, then the code associated with it is never registered for execution at program termination. If an END statement is within a loop and is executed more than once, then the code associated with it is still only registered once:

a = 4;
if (true) 
  END {                        # This END is executed
    puts "if";                 # This code is registered
    puts a                     # The variable is visible; prints "4"
  }
else
  END { puts "else" }          # This is not executed
end
10.times {END { puts "loop" }} # Only executed once

The Kernel method at_exit provides an alternative to the END statement; it registers a block of code to be executed just before the interpreter exits. As with END blocks, the code associated with the first at_exit call will be executed last. If the at_exit method is called multiple times within a loop, then the block associated with it will be executed multiple times when the interpreter exits.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.220.92