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"
BEGIN{ print "OnInit(object sender, EventArgs args)\n" } BEGIN{ print "OnLoad(object sender, EventArgs args)\n" } print "Running"
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"
print "OnLoad(object sender, EventArgs args)\n" BEGIN{ print "OnInit(object sender, EventArgs args)\n" } print "Running"
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
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-06Detailed 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-04Ruby exception handling: ensure
Ruby exception handling: ensure...2007-11-11Ruby 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-01Ruby some simple examples
Ruby some simple examples...2007-11-11Introduction 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-01Rails link_to Detailed explanation
Friends who want to learn rauks link_to can refer to the following example.2008-12-12Easy 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-08ruby object-oriented thinking concept
ruby object-oriented thinking concept...2007-11-11Luhn 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