Ruby Conditionals
Within Ruby scripts, Conditional statements are used for logical flow control. A Conditional checks whether a predicate is true or false, and executes the written code based on the result of the check. They rely heavily on the boolean data type, i.e. true/false.
Writing conditional statements requires two understandings:
-
How to articulate a condition which evaluates to a boolean, and
-
How to control the flow of script execution
Predicates
A predicate is an expression which evaluates to true or false. Depending on the datatype which is being tested, different questions may be asked of it which evaluate to true or false. Moreover, it is likely a programmer will write custom predicates specific to their program and datatypes. For the purpose of introducing conditional flow control, this manual section will introduce Integer comparison predicates.
| Symbol | Meaning | Returns |
|---|---|---|
| > | is greater than | true or false |
| < | is less than | true or false |
| == | is equal to | true or false |
Notice that the equality symbol, also called a double-equals, requires the use of two equal signs. Remember, a single equals sign is used for variable assignment. The double-equals must be used to induce a boolean.
Similar to standard mathematics, each of the given symbols require a left and right element to become a valid predicate expression. When the script is executed, a value of true or false can be thought of as replacing the expression and guiding execution flow.
Comparisonsputs 1 > 2
puts 1 == 2
puts 1 < 2
false
false
true
Flow Control 1: If
The if statement checks if the condition which it is passed is true. If it is true, expressions within the if clause are executed.
The standard syntax for an if statement in Ruby begins with the keyword if followed by a predicate expression, then some number of newlines with indention indicating the expressions which should execute if the predicate is true, and ends with the keyword end.
IF STATEMENTif predicate
puts 'if true, this prints'
end
Flow Control 2: Extended If
The if statement may be extended to capture different logical conditions. This may occur if the programmer would like to control flow by stating "if this, do this, otherwise do this other thing." The keywords for writing an extended if statement are if, elsif, and else.
Similar to if, the elsif keyword requires a predicate to be valid Ruby code. The else keyword does not use an inline predicate, and will execute its inner expressions if none of the previous predicates evaluate to true. The entire extended if statement must still end with the end keyword.
IF/ELSIF/ELSE STATEMENTif predicate
puts 'if true, this prints'
elsif predicate2
puts 'the second predicate is true'
else
puts 'this will print if none of the above are true'
end
Flow Control 3: Ternary
The Ternary conditional is a terse if statement, and is often shortened to one line. It starts with a predicate followed by a space and a question mark (?), then an expression to be evaluated if the condition is true followed by a colon (:), and finishing with an expression which is to be evaluated if the predicate returns false.
A ternary expression can be used at the left side of variable definition if the value of the variable depends on some condition.
TERNARY STATEMENT1 == 1 ? 'true' : 'false'
Flow Control 4: Loops
The Loop construct is used to ensure that an expression block is evaluated repeatedly while some condition evaluates to true. The Loop construct, generally, can be simplified to "Do this, until it's complete," or the inverse statement "While it's incomplete, do this."
An important consideration with loop is that it should end at some point otherwise it is an infinite loop. Such a loop will cause the program to never stop executing, an undesired effect. To ensure the loop exits at some point, there are two options.
-
Within the loop expression, be sure to modify the variables used within the loop predicate such that eventually the predicate evaluates to false.
-
Within the loop expression, use the break keyword, which explicitly stops execution of loop expression and breaks out of the loop.
The first type of loop shown here satisfies the statement "Do this until it's complete." The loop starts with the keyword begin which is followed by a newline and some number of indented expressions which are considered to be the loop expressions. The loop expressions end with the end keyword. Importantly, end is followed by the while keyword along with the predicate which is evaluated to see if the loop expression should be evaluated again.
BEGIN/WHILE LOOPinteger = 3
begin
puts integer
integer = integer -- 1
end while integer > 0
3
2
1
An important consideration with the begin..while loop, is that the loop expression will always be evaluated once even if the predicate is initially false.
In contrast, the loop structure which satisfies "While it's incomplete, do this," will only ever enter the loop expression if the predicate evaluates to true from the start. This loop starts with the while keyword, followed by the predicate, the loop expressions, and finally the end keyword.
WHILE LOOPinteger = 1
while integer < 4
puts integer
integer = integer + 1
end
1
2
3