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