Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

5.7. 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:


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial