Ruby Flow Control

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Ruby Object Oriented ProgrammingThe Ruby case Statement


Purchase and download the full PDF and ePub editions of this Ruby eBook for only $8.99


One of the most powerful features of Ruby (and every other programming or scripting language for that matter) is the ability to build intelligence and logic into code. This is achieved through the use of control structures which decide what code is executed based on logical expressions.

In this chapter of Ruby Essentials we will look at how such control structures are built.


Contents


The Ruby if Statement

The if statement is the most basic of the Ruby control structures. if statements simply specify a section of Ruby code to be executed when a specified criteria is met. The syntax for an if statement is as follows:


if expression then
  ruby code
end


In the above outline, the expression is a logical expression that will evaluate to either true or false. If the expression is true, then the code represented by ruby code will execute. Otherwise the code is skipped. The end statement marks the end of the if statement.

Let's look at an example:

if 10 < 20 then
    print "10 is less than 20"
end

When executed, the code will display the string "10 is less than 20", because the 10 < 20 expression evaluated to true.

If this was any language other than Ruby we would now move on to the next section. Except that this is Ruby we are talking about, so we have more flexibility. Firstly, we can drop the then keyword and get the same result:

if 10 < 20
    print "10 is less than 20"
end

We can also place the if after the print statement, and in doing so, drop the end statement:

print "10 is less than 20" if 10 < 20

Similarly, we can place a colon (:) between the if expression and the statement to be executed:

if 10 < 20: print "10 is less than 20" end

The expression to be evaluated can also include logical operators. For example:

if firstname == "john" && lastname == "smith" then
print "Hello John!"
end

Using else and elsif Constructs

The if control structure outlined above allows you to specify what should happen if a particular expression evaluates to true. It does not, however, provide the option to specify something else that should happen in the event that the expression evaluates to false. This is where the if ... else construct comes into play.

The syntax for if ... else is similar to the simple if statement with the exception that the else statement can be used to specify alternate action:

if customerName == "Fred"
      print "Hello Fred!"
else 
      print "You're not Fred! Where's Fred?"
end

As shown in the above example the code block following the if statement is executed when the expression is evaluated to be true (i.e. the customerName variable contains the string "Fred") and the script after the else statement is executed when the customerName does not match the string "Fred".

elsif structures take the if ... else ... concept a step further to implement if ... else ... if ... else ... structures. For example:

if customerName == "Fred"
      print "Hello Fred!"
elsif customerName == "John"
      print "Hello John!" 
elsif customername == "Robert"
      print "Hello Bob!"
end

This construct may also be expressed a little more economically using a colon (:) to separate the elsif from the print statement:

if customerName == "Fred" : print "Hello Fred!"
elsif customerName == "John" : print "Hello John!" 
elsif customername == "Robert" : print "Hello Bob!"
end

The Ruby Ternary Operator

Ruby uses something called a ternary operator to provide a shortcut way of making decisions. The syntax of the ternary operator is as follows:

[condition] ? [true expression] : [false expression]

The way this works is that [condition] is replaced with an expression that will return either true or false. If the result is true then the expression that replaces the [true expression] is evaluated. Conversely, if the result was false then the [false expression] is evaluated. Let's see this in action:

irb(main):182:0> customerName = "Fred"
=> "Fred"
irb(main):183:0> customerName == "Fred" ? "Hello Fred" : "Who are you?"
=> "Hello Fred"

Summary

In this chapter of Ruby Essentials we have explored basic control structures which allow us to build decision making logic into our Ruby programs. In the following chapter we will look at a more advanced conditional structure, The Ruby case Statement.


Purchase and download the full PDF and ePub editions of this Ruby eBook for only $8.99



PreviousTable of ContentsNext
Ruby Object Oriented ProgrammingThe Ruby case Statement