Doll duck learn ruby-5, blank space character

Source: Internet
Author: User

Blank characters include space characters, tabs, and line breaks. They are not tags, but are used to separate different tags, so that they are not merged into a tag.

Note:
Sometimes blank characters are required, and some are actually forbidden.

1. linefeed used as the statement Terminator
Each statement in C and Java must end with a semicolon. In Ruby, you can use semicolons to terminate the statement, but this is not necessary. Only when you tryCodeMultiple statements must be separated by semicolons. In addition, the semicolon is omitted according to the Convention.

Ruby interpreter intelligence:
At the end of an explicit semicolon, the Ruby interpreter looks for its own end.
If a line of Ruby code is a complete syntax statement, Ruby regards the line break at the end of the line as the terminator of the language name. Otherwise, Ruby will continue to parse the Statement of the next line, until a complete statement is obtained.
There will be problems with intelligence:
If multiple statements are distributed, the statement may be incorrectly interpreted.
Total = x +
Y
----------------------------------------
Ruby will explain it correctly
However
Total = x
+ Y
----------------------------------------
It will be interpreted as two expressions. Total = x assigns the value of X to total. downlink + Y is meaningless, but it is a complete expression.

The return and break statements may be incorrectly interpreted.

Summary:
Line breaks can be ensured in the following situations
1) An operator is followed by a line break, as shown in figure
Puts x +
Y
2) Insert a line break after the period of the method call
Foo. New.
Say_hello
3) comma used to separate each element in the array or hash table word volume
[1, 2, 3] or {x => 1, y => 1}
Insert the following line feed:
[1,
2,
3]
{X => 1,
Y => 1}
There will be no problems in the above two cases
4) you can use a "/" to escape line breaks. This prevents ruby from automatically ending the statement:
VaR Total = first_long_variable_name + second_long_variable_name/
+ Third_long_variable_name # Note No statement Terminator above
In Ruby 1.9, the statement Terminator rule has changed slightly: if the first non-blank character in a line of code is a period, this line will be treated as a continuation of the previous line, in addition, the line break before the row statement is not treated as the statement Terminator.
Animals = array. New
. Push ("dog") # Not in Ruby 1.8
. Push ("cow ")
. Push ("cat ")
. Sort

2. space characters and method calls
Ruby syntax allows parentheses related to method calls to a specific environment to be omitted, which makes Ruby methods use the same way as statements.
Elegance ??
Hazard ?? Complementing each other
F (3 + 2) + 1
F (3 + 2) + 1
Are there differences between the two? Spaces are not ignored
----------------------------------------
In Ruby, F (3 + 2) + 1 passes the result 5 of (3 + 2) to method F, and then adds 1 to the result.
F (3 + 2) + 1 indicates that there is a space behind the method name. The expression 3 + 2 enclosed by parentheses after the space character is calculated first, then pass the result of (3 + 2) + 1 to function f.

F (3 + 2) + 1 => equivalent to F (5) + 1
F (3 + 2) + 1 => equivalent to F (6)
The difference is obvious.

If you enable the warning using the-W parameter when executing the code, Ruby will issue a warning when it encounters a code with ambiguity.
Solution:
× Never leave the method name and left parentheses after it blank
× If the first parameter of a method starts with parentheses, Parentheses (f (3 + 2) + 1) are always used in this method call)
× Please keep using the-W option of the Ruby interpreter so that it will issue a warning when you forget the above rules.

 

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.