The learning of Ruby is the same as that of other programming languages. Here we will introduce how to write the in and END in the basic Ruby code.
- Analysis of Ruby encryption implementation code examples
- Ruby blocks provides flexible encoding methods
- Parse Json IN Ruby
- Ruby module Win32API calls win32API directly
- How to call win32ole IN Ruby
Basic Ruby code: BEGIN Block
Code in the BEGIN block is executed before all code is executed. Ruby allows you to set multiple BEGIN blocks and execute the code in the blocks in the displayed order. C # note 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 pretty good. Unfortunately, the above Code segment will have a 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 shown in the preceding code snippet, the code can be correctly executed only when the starting braces and the in identifier are in the same row block. 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 only once.
- I = 0
- While I <10
- # While processing the loop structure
The code in 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,
As long as the BEGIN block appears, it will be executed.
- BEGIN {
- Print "OnLoad (object sender,
EventArgs args) \ n"
- }
- End
- Print "Running"
Based on the principle that only BEGIN is executed and BEGIN is executed before all code is executed, even if the code appears before the BEGIN block, the code will still be executed after the BEGIN block is executed. For example, the output result 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 basic code
The END block is opposite to the BEGIN block. It is executed after all code is executed. When multiple END blocks are executed, the first END block is executed. In addition, although the END block is not affected by the while, the if block may be used to control the execution of the END block. For example, the output result of the following code is Start-Load-Unload.
- If false
- END {
- # Never output
- Print "Init"
- }
- End
- END {
- # Final output
- Print "Unload \ n"
- }
- END {
- # Output before Unload
- Print "Load \ n"
- }
- # First output
- Print "Start \ n"
The preceding section describes the basic Ruby code.