Ruby has a variety of ways to control execution. All the expressions
described here return a value.

For the tests in these control expressions, nil and
false are false-values and true and any other
object are true-values. In this document “true” will mean “true-value” and
“false” will mean “false-value”.

You may also write a if-then-else expression using ? and
:. This ternary if:

input_type = gets=~/hello/i?"greeting":"other"

Is the same as this if expression:

input_type =
ifgets=~/hello/i"greeting"else"other"end

While the ternary if is much shorter to write than the more verbose form,
for readability it is recommended that the ternary if is only used for
simple conditionals. Also, avoid using multiple ternary conditions in the
same expression as this can be confusing.

if and unless can also be used to modify an
expression. When used as a modifier the left-hand side is the “then”
expression and the right-hand side is the “test” expression:

a = 0a+=1ifa.zero?pa

This will print 1.

a = 0a+=1unlessa.zero?pa

This will print 0.

While the modifier and standard versions have both a “test” expression and
a “then” expression, they are not exact transformations of each other due
to parse order. Here is an example that shows the difference:

The most common way is to compare an object against multiple patterns. The
patterns are matched using the +===+ method which is aliased to +==+ on Object. Other classes must override it to give
meaningful behavior. See Module#=== and Regexp#=== for examples.

Here is an example of using case to compare a String against a
pattern:

Prints the numbers 0 through 10. The condition a < 10 is
checked before the loop is entered, then the body executes, then the
condition is checked again. When the condition results in false the loop
is terminated.

The do keyword is optional. The following loop is equivalent
to the loop above:

whilea<10paa+=1end

The result of a while loop is nil unless
break is used to supply a value.

This prints the numbers 0 through 11. Like a while loop the condition
a > 10 is checked when entering the loop and each time the
loop body executes. If the condition is false the loop will continue to
execute.

Like a while loop, the do is optional.

Like a while loop, the result of an until loop is
nil unless break is used.

In Ruby 1.8, you could also use retry where you used
redo. This is no longer true, now you will receive a SyntaxError when you use retry
outside of a rescue block. See Exceptions for proper usage of
retry.

The flip-flop is a rarely seen conditional expression. It's primary
use is for processing text from ruby one-line programs used with ruby
-n or ruby -p.

The form of the flip-flop is an expression that indicates when the
flip-flop turns on, .. (or ...), then an
expression that indicates when the flip-flop will turn off. While the
flip-flop is on it will continue to evaluate to true, and
false when off.

In the above example, the on condition is n==2. The flip-flop
is initially off (false) for 0 and 1, but becomes on (true) for 2 and
remains on through 8. After 8 it turns off and remains off for 9 and 10.

The flip-flop must be used inside a conditional such as if,
while, unless, until etc. including
the modifier forms.

When you use an inclusive range (..), the off condition is
evaluated when the on condition changes: