2.1 The if..else construct

Ruby Conditional Branching : the 'if' statement

You can use Ruby's Boolean Expressions to specifiy conditions using the usual if..else construct. Let us take a look at a simple example:

Example Code:

Output Window

When you run the above example, it will tell you that 0 is negative. But we know that zero is neither positive nor negative. How do we fix this program so that zero is treated separately?

Ruby gives you the elsif keyword that helps you check for multiple possibilities inside an if..else construct. Try using elsif to fix the code below so that when the number is zero, it just prints 0.


            # do this and that
            # something different.
            # none of the above worked. this is where it finally ends.

Output Window

Ruby also has an unless keyword that can be used in places where you want to check for a negative condition. unless x is equivalent to if !x. Here is an example:

Example Code:

Output Window

The ternary operator

In english, "ternary" is an adjective meaning "composed of three items." In a programming language, a ternary operator is simply short-hand for an if-then-else construct.

In Ruby, ? and : can be used to mean "then" and "else" respectively. Here's the first example on this page re-written using a ternary operator.
Example Code:

Output Window

Truthiness of objects in Ruby

The conditional statements if and unless can also use expressions that return an object that is not either true or false.

In such cases, the objects false and nil equates to false. Every other object like say 1, 0, "" are all evaluated to be true. Here is a demonstration:

Output Window

Congratulations, guest!

% of the book completed


This lesson is Copyright © 2011-2014 by Sidu Ponnappa and Jasim A Basheer