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

Regular Expressions in Boolean Expressions

If a single regular expression appears as a boolean expression, it is matched against the current value of the variable $_.

if /re/ ...

is equivalent to

if $_ =~ /re/ ...

If and Unless Expressions

ifandunlessExpressions

if boolean-expression[then]

body

elsif boolean-expression[then]

body

else

body

end

unless boolean-expression[then]

body

else

body

end

The thenkeyword separates the body from the condition. It is not required if the body starts on a new line. The value of aniforunlessexpression is the value of the last expression evaluated in whichever body is executed.

If and Unless Modifiers

expressionifboolean-expression

expressionunlessboolean-expression

evaluates expressiononly ifboolean-expressionistrue(falseforunless).

Ternary Operator

boolean-expression?expr1:expr2

returns expr1ifboolean expressionis true andexpr2otherwise.

Case Expressions

caseExpressions

case target

when [

comparison

]+

[then]

body

when [

comparison

]+

[then]

body

...

[else

body]

end

A case expression searches for a match by starting at the first (top left) comparison, performing comparison===target. When a comparison returns true, the search stops, and the body associated with the comparison is executed.casethen returns the value of the last expression executed. If nocomparisonmatches: if anelseclause is present, its body will be executed; otherwise,casesilently returnsnil.

The thenkeyword separates thewhencomparisons from the bodies, and is not needed if the body starts on a new line.

Loops

Loop Constructs

while boolean-expression[do]

body

end

executes bodyzero or more times as long asboolean-expressionis true.

until boolean-expression[do]

body

end

executes bodyzero or more times as long asboolean-expressionis false.

In both forms, the doseparatesboolean-expressionfrom thebody, and may be omitted when the body starts on a new line.

for [

name

]+

in expression[do]

body

end

The forloop is executed as if it were the followingeachloop, except that local variables defined in the body of theforloop will be available outside the loop, while those defined within an iterator block will not.

expression.each do |[

name

]+

|

body

end

loop, which iterates its associated block, is not a language construct---it is a method in moduleKernel.

While and Until Modifiers

expressionwhileboolean-expression

expressionuntilboolean-expression

If expressionis anything other than abegin/endblock, executesexpressionzero or more times whileboolean-expressionistrue(falseforuntil).

If expressionis abegin/endblock, the block will always be executed at least one time.

Break, Redo, Next, and Retry

break,redo,next, andretryalter the normal flow through awhile,until,for, or iterator controlled loop.

breakterminates the immediately enclosing loop---control resumes at the statement following the block.redorepeats the loop from the start, but without reevaluating the condition or fetching the next element (in an iterator).nextskips to the end of the loop, effectively starting the next iteration.retryrestarts the loop, reevaluating the condition.

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