Ruby basic code Experience Sharing

Source: Internet
Author: User
Tags parse error

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

  1. BEGIN
  2. {
  3. Print "OnInit (object sender,
    EventArgs args) \ n"
  4. }
  5. BEGIN
  6. {
  7. Print "OnLoad (object sender,
    EventArgs args) \ n"
  8. }
  9. Print "Running"

The above code looks pretty good. Unfortunately, the above Code segment will have a parse error. The correct code should be

 
 
  1. BEGIN{  
  2. print "OnInit(object sender, 
    EventArgs args)\n"  
  3. }  
  4. BEGIN{  
  5. print "OnLoad(object sender,
     EventArgs args)\n"  
  6. }  
  7. 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.

 
 
  1. I = 0
  2. While I <10
  3. # While processing the loop structure
    The code in the block is still executed only once.
  4. BEGIN {
  5. Print "OnInit (object sender,
    EventArgs args) \ n"
  6. }
  7. I + = 1
  8. End
  9. If false
  10. # BEGIN is completely unaffected by if,
    As long as the BEGIN block appears, it will be executed.
  11. BEGIN {
  12. Print "OnLoad (object sender,
    EventArgs args) \ n"
  13. }
  14. End
  15. 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.

 
 
  1. print "OnLoad(object sender, 
    EventArgs args)\n"  
  2. BEGIN{  
  3. print "OnInit(object sender,
     EventArgs args)\n"  
  4. }  
  5. 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.

 
 
  1. If false
  2. END {
  3. # Never output
  4. Print "Init"
  5. }
  6. End
  7. END {
  8. # Final output
  9. Print "Unload \ n"
  10. }
  11. END {
  12. # Output before Unload
  13. Print "Load \ n"
  14. }
  15. # First output
  16. Print "Start \ n"

The preceding section describes the basic Ruby code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.