The Ruby case Statement

From Techotopia
Revision as of 15:07, 19 March 2009 by Neil (Talk | contribs)

Jump to: navigation, search
PreviousTable of ContentsNext
Ruby Flow ControlRuby While and Until Loops



Contents


Introduction

In this chapter of Ruby Essentials we will look at implementing flow control logic in Ruby scripts.

Ruby Flow Control

In the previous chapter we looked at some basic conditional structures using the if ... else and if .. elsif ... mechanisms. These approaches to building conditional logic work well if you need to check a value against a only few different criteria (for example checking the value of a string against a couple of possible candidates):

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

This can quickly become cumbersome, however, when a need arises to evaluate a large number of conditions. A much easier way to handle such situations is to use the Ruby case statement, the syntax for which is defined as follows:

result = case value
   when match1: result1
   when match2: result2
   when match3: result3
   when match4: result4
   when match5: result5
   when match6: result6
   else result7
end


There can be any number of when statements - basically as many as you need to fully compare the value in the case statement against the possible options (represented by match1 through to match7 in the above example) specified by the when statements. When a match is found the result is assigned to the optional result variable.

Finally, the else statement specifies the default result to be returned if no match is found.

This concept is, perhaps, best explained using an example. The following Ruby case statement is designed to match a particular car model with a manufacturer. Once a match is found, the car and associated manufacturer are included in an output string:

car = "Patriot"

manufacturer = case car
   when "Focus": "Ford"
   when "Navigator": "Lincoln"
   when "Camry": "Toyota"
   when "Civic": "Honda"
   when "Patriot": "Jeep"
   when "Jetta": "VW"
   when "Ceyene": "Porsche"
   when "Outback": "Subaru"
   when "520i": "BMW"
   when "Tundra": "Nissan"
   else "Unknown"
end

puts "The " + car  + " is made by "  + manufacturer

When executed, the resulting output will read:

The Patriot is made by Jeep

If no match was found in the case statement, then the default result, defined by the else statement would cause the following output to be generated:

The Prius is made by Unknown

Number Ranges and the case statement

The case statement is also particularly useful when used in conjunction with number ranges (for details of ranges read the Ruby Ranges chapter of this book).

The following case example detects which where a number falls amongst a group of different ranges:

score = 70

result = case score
   when 0..40: "Fail"
   when 41..60: "Pass"
   when 61..70: "Pass with Merit"
   when 71..100: "Pass with Distinction"
   else "Invalid Score"
end

puts result

The above code, when executed will result in a "Pass with Merit" message.

Summary

The if .. else ... approach to building conditional logic into an application works fine for evaluating a limited number of possible criteria. For much larger evaluations, the Ruby case statement is a less cumbersome alternative. In the chapter we have looked at the case statement and reviewed some examples using strings and number ranges.



PreviousTable of ContentsNext
Ruby Flow ControlRuby While and Until Loops