Simple Constructs

Let's explore some very simple constructs available in Ruby. The example below p014constructs.rb illustrates the if else end construct. By the Ruby convention, if and while do not require parenthesis.

An example of using elsif is there in the program p015elsifex.rb as shown below:

Some common conditional operators are: ==, != >=, <=, >, <

unless, as a statement or a modifier, is the opposite of if: it executes code only if an associated expression evaluates to false or nil. Ruby's unless construct begins with unless and ends with end. The body is the text between the two.

In the above program, the body is executed unless the number of elements in the array is equal to 2 (meaning that both arguments were given). The method Kernel.exit terminates your program, returning a status value to the operating system.

Loops like the while loop are available. Again, the example below illustrates the same.

Conditional ?:

As a concise alternative to simple if/else statements we can use the conditional or ternary ?: operator. It is the only ternary operator (three operands) in Ruby. It has the following basic structure:

The first operand appears before the question mark. The second operand appears between the question mark and the colon. An the third operand appears after the colon. The question mark must appear on the same line as the first argument and the colon must appear on the same line as the second argument. The ?: operator always evaluates its first operand. If the first operand is anything other than false or nil, the value of the expression is the value of the second operand. Otherwise, if the first operand is false or nil, then the value of the expression is the value of the third operand. The ?: operator acts like a compact if/then/else statement. Let's look at an example:

The ternary operator also can be useful for conditional assignments:

Statement modifiers

Ruby statement modifiers are a useful shortcut if the body of an if or while statement is just a single expression. Simply write the expression, followed by if or while and the condition. For example, here's a simple if statement.

Case Expressions

This form is fairly close to a series of if statements: it lets you list a series of conditions and execute a statement corresponding to the first one that's true. For example, leap years must be divisible by 400, or divisible by 4 and not by 100. Also, remember that case returns the value of the last expression executed.

nil is an Object

In Ruby, nil is an actual object. You can call methods on nil, just like any other object. You can add methods to nil, just like any other object.

In Ruby, nil and false evaluate to false, everything else (including true, 0) means true.

Difference between FALSE and NIL

Though we still have to talk about classes, nevertheless here is some additional information for you. nil and false are not the same things. Both have a false value and also remember that everything in Ruby is an object. See the following program:

Note: The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. I have made extensive references to information, related to Ruby, available in the public domain (wikis and the blogs, articles of various Ruby Gurus), my acknowledgment and thanks to all of them. Much of the material on and in the course at is drawn primarily from the Programming Ruby book, available from The Pragmatic Bookshelf.