SoFunction
Updated on 2025-04-09

Introduction to Ruby Page 2/5


2. BEGIN and END
BEGIN block


Code in BEGIN blocks is executed before all code is executed, and Ruby allows multiple BEGIN blocks to be set up and the code in the blocks is executed in the order in which they appear. C# programmers pay attention to the following code
BEGIN { print "OnInit(object sender, EventArgs args)\n" } BEGIN { print "OnLoad(object sender, EventArgs args)\n" } print "Running"
The above code looks beautiful, but unfortunately, the above code segment will have parse error. The correct code should be
BEGIN{ print "OnInit(object sender, EventArgs args)\n" } BEGIN{ print "OnLoad(object sender, EventArgs args)\n" } print "Running"
As presented in the above code snippet, code within the block can be executed correctly only when the starting braces and the BEGIN identifier are on the same line. At the same time, the BEGIN block is not affected by any control structure, because as long as the BEGIN block appears, it will be executed and only once.
1i = 0 2while i < 10 3 # Although the loop structure is processed, the code in the BEGIN block is still executed only once 4 BEGIN{ 5 print "OnInit(object sender, EventArgs args)\n" 6 } 7 i += 1 8end 9 10if false 11 # BEGIN is completely unaffected by if, and will be executed as long as the BEGIN block appears. 12 BEGIN{ 13 print "OnLoad(object sender, EventArgs args)\n" 14 } 15end 16 17print "Running"
Based on the principle that BEGIN will be executed and BEGIN will be executed before all code execution, even if the code appears before the BEGIN block, the code will still wait for the BEGIN block to be executed before execution. For example, the output result of the following code segment is still OnInit - OnLoad - Running.
print "OnLoad(object sender, EventArgs args)\n" BEGIN{ print "OnInit(object sender, EventArgs args)\n" } print "Running"
END block

The END block is contrary to the BEGIN block. It is executed after all code is executed, and the END block that appears first when multiple END blocks are executed last. In addition, although the END block is not affected by while, it may use if to control whether the END block is executed. For example, the output result of the following code is Start - Load - Unload.
if false END{ # Never output print "Init" } end END{ # Last output print "Unload\n" } END{ # Output before Unload print "Load\n" } # First output print "Start\n"
Previous page12345Next pageRead the full text
  • Introduction to Ruby

Related Articles

  • Installing bundler in Ruby environment to manage multiple versions of gems

    This article mainly introduces the method of installing bundler to manage multi-version gems in the Ruby environment. It gives an application example in Ruby On Rails for demonstration. Friends who need it can refer to it.
    2016-06-06
  • Detailed explanation of Ruby regular expressions

    Regular expressions are special sequence characters. They match or find string collections by using patterns with special syntax. This article introduces Ruby's large regular expressions in detail. The article has detailed code examples. Friends who need it can refer to it.
    2023-04-04
  • Ruby exception handling: ensure

    Ruby exception handling: ensure...
    2007-11-11
  • Ruby metaprogramming summary

    This article mainly introduces the Ruby metaprogramming summary. Metaprogramming is a technology that can operate language structures (such as classes, modules, instance variables, etc.) dynamically at runtime. Friends who need it can refer to it.
    2015-01-01
  • Ruby some simple examples

    Ruby some simple examples...
    2007-11-11
  • Introduction to the Google Map/Reduce framework in Ruby

    This article mainly introduces the Google Map/Reduce framework in Ruby. Skynet is a framework for creating distributed applications. Friends who need it can refer to it.
    2015-01-01
  • Rails link_to Detailed explanation

    Friends who want to learn rauks link_to can refer to the following example.
    2008-12-12
  • Easy Ways to Block Spam in Blog

    This article mainly introduces a simple way to block spam messages in your blog. The author takes the blog application built by Ruby on Rails as an example. Friends who need it can refer to it.
    2015-08-08
  • ruby object-oriented thinking concept

    ruby object-oriented thinking concept...
    2007-11-11
  • Luhn algorithm learning and its Ruby version implementation code example

    The Luhn algorithm is mainly used for digital verification, especially card number, ID number, etc. Here we will take a look at the Luhn algorithm learning and its Ruby version implementation code example:
    2016-05-05

Latest Comments