Control Expressions

Control Expressions

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”.

if Expression

The simplest if expression has two parts, a “test” expression and a “then” expression. If the “test” expression evaluates to a true then the “then” expression is evaluated.

Here is a simple if statement:

if true then
  puts "the test resulted in a true-value"
end

This will print “the test resulted in a true-value”.

The then is optional:

if true
  puts "the test resulted in a tr