In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Ruby basic code example analysis, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Learning the Ruby language, like other programming languages, starts with the basics. Here we introduce how to write BEGIN and END in the basic code of Ruby.
BEGIN block of Ruby base code
The code in the BEGIN block executes before all code is executed, and Ruby allows you to set multiple BEGIN blocks and execute the code in the block in the order in which it appears. 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 there will be parse error in the above code snippet. The correct code should be
BEGIN {
Print "OnInit (object sender
EventArgs args)\ n "
}
BEGIN {
Print "OnLoad (object sender
EventArgs args)\ n "
}
Print "Running"
As the code snippet above shows, the code within the block can be executed correctly only if the opening curly braces and the BEGIN identifier are on the same line. At the same time, BEGIN blocks are not affected by any control structure, because whenever BEGIN blocks appear, they are executed and executed only once.
I = 0
While I < 10
# although in the processing loop structure, BEGIN
The code within the block is still executed only once
BEGIN {
Print "OnInit (object sender
EventArgs args)\ n "
}
I + = 1
End
If false
# BEGIN is completely unaffected by if
Whenever a BEGIN block appears, it will be executed.
BEGIN {
Print "OnLoad (object sender
EventArgs args)\ n "
}
End
Print "Running"
Based on the principle that the BEGIN is executed as soon as it appears and the BEGIN executes before all the code is executed, even if the code appears in front of the BEGIN block, the code still waits for the BEGIN block to execute. For example, the output of the following code snippet is still OnInit-OnLoad-Running.
Print "OnLoad (object sender
EventArgs args)\ n "
BEGIN {
Print "OnInit (object sender
EventArgs args)\ n "
}
Print "Running"
END block of Ruby base code
END blocks, in contrast to BEGIN blocks, are executed after all code is executed, and the END blocks that appear first in multiple END blocks are executed last. In addition, although END blocks are not affected by while, it is possible to control the execution of END blocks through if. For example, the output of the following code is Start-Load-Unload.
If false END {# never output print "Init"} end END {# Last output print "Unload\ n"} END {# first output print "Load\ n"} # first output print "Start\ n" the answer to the sample analysis question on Ruby basic code is shared here. I hope the above content can be of some help to you, if you still have a lot of questions unsolved. You can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.