Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ruby / Yukihiro Matsumoto_Programming Ruby.doc
Скачиваний:
122
Добавлен:
06.06.2015
Размер:
2.71 Mб
Скачать

Block Expressions

begin

body

end

Expressions may be grouped between beginandend. The value of the block expression is the value of the last expression executed.

Block expressions also play a role in exception handling, which is discussed starting on page 235.

Boolean Expressions

Boolean Expressions

Boolean expressions evaluate to a truth value. Some Ruby constructs (particularly ranges) behave differently when evaluated in a boolean expression.

Truth Values

Ruby predefines the globals falseandnil. Both of these values are treated as being false in a boolean context. All other values are treated as being true.

And, Or, Not, and Defined?

The andand&&operators evaluate their first operand. If false, the expression returns false; otherwise, the expression returns the value of the second operand.

expr1andexpr2

expr1&&expr2

The orand||operators evaluate their first operand. If true, the expression returns true; otherwise, the expression returns the value of the second operand.

expr1orexpr2

expr1||expr2

The notand!operators evaluate their operand. If true, the expression returns false. If false, the expression returns true.

The word forms of these operators (and,or, andnot) have a lower precedence than the corresponding symbol forms (&&,||, and!). See Table 18.4 on page 219 for details.

The defined?operator returnsnilif its argument, which can be an arbitrary expression, is not defined. Otherwise, it returns a description of that argument. For examples, see page 78 in the tutorial.

Comparison Operators

The Ruby syntax defines the comparison operators ==,===,<=>,<,<=,>,>=,=~, and the standard methodseql?andequal?(see Table 7.1 on page 79). All of these operators are implemented as methods. Although the operators have intuitive meaning, it is up to the classes that implement them to produce meaningful comparison semantics. The library reference starting on page 275 describes the comparison semantics for the built-in classes. The moduleComparableprovides support for implementing the operators==,<,<=,>,>=, and the methodbetween?in terms of<=>. The operator===is used incaseexpressions, described on page 223.

Both ==and=~have negated forms,!=and!~. Ruby converts these during syntax analysis:a!=bis mapped to!(a==b), anda!~bis mapped to!(a =~b). There are no methods corresponding to!=and!~.

Ranges in Boolean Expressions

if expr1..expr2

while expr1...expr2

A range used in a boolean expression acts as a flip-flop. It has two states, set and unset, and is initially unset. On each call, the range cycles through the state machine shown in Figure 18.1 on page 223. The range returns trueif it is in the set state at the end of the call, andfalseotherwise.

The two-dot form of a range behaves slightly differently than the three-dot form. When the two-dot form first makes the transition from unset to set, it immediately evaluates the end condition and makes the transition accordingly. This means that if expr1andexpr2both evaluate totrueon the same call, the two-dot form will finish the call in the unset state. However, it still returnstruefor this call.

The difference is illustrated by the following code:

a = (11..20).collect {|i| (i%4 == 0)..(i%3 == 0) ? i : nil}

a

»

[nil, 12, nil, nil, nil, 16, 17, 18, nil, 20]

a = (11..20).collect {|i| (i%4 == 0)...(i%3 == 0) ? i : nil}

a

»

[nil, 12, 13, 14, 15, 16, 17, 18, nil, 20]

Figure not available...

Соседние файлы в папке Ruby