Doll duck learn ruby-6, syntax structure

Source: Internet
Author: User
6.1 expression
Primary Expression
Numbers and strings are basic expressions.
Specific Keyword: True False nil self
The reference to variables is also a basic expression. The result of their evaluation is the value of the variable to which it points.

Compound expression
[1, 2, 3] # Array
{1 => "one", 2 => "two"} # hash
1. 3 # A range literal

Operators are used to calculate multiple values. Operators are used to combine simpler subexpressions to form a composite expression.
1 # Primary Expression
X # Another basic expression
X = 1 # assignment
X = x + 1 # expression of two operators

Statement
The expression and the ruby keyword are combined to form a statement.
If x <10 then
X = x + 1
End

While x <10 do
Print x
X = x + 1
End

Method
Method groups for class interoperability
Associated classes and some methods independent of those classes are organized into modules

6.2 Block Structure
Block Structure
Modules, classes, method definitions, and most Ruby statements contain nestedCodeBlock.
These blocks are separated by keywords or punctuation marks. By convention, the indentation of two space characters is used.

Block)
3. Times {print "Ruby! "}

1. upto (10) Do | x |
Print x
End

Generally, the DO and end delimiters are used when the Block Code contains more than one line. Two-space indent

body
Statement List, including the definition body, method definition body, while loop body, and other structures.
Note:
the ruby body never uses curly brackets as the separator, but keywords.
module stats
class dataset
def initialize (filename)
Io. foreach (filename) do | Line |
If line [0, 1] = "#"
next
end
end

Danny

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.