The Ruby case Statement

Follow Techotopia on Twitter Bookmark and Share

From Techotopia

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

Purchase and download the PDF and ePub versions of this Ruby eBook for only $8.99  Add to Cart

In this chapter of Ruby Essentials we will look at implementing flow control logic in Ruby scripts through the use of the Ruby case statement.


[edit] 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 only a 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!"

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 then result1
   when match2 then result2
   when match3 then result3
   when match4 then result4
   when match5 then result5
   when match6 then result6
   else result7

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" then "Ford"
   when "Navigator" then "Lincoln"
   when "Camry" then "Toyota"
   when "Civic" then "Honda"
   when "Patriot" then "Jeep"
   when "Jetta" then "VW"
   when "Ceyene" then "Porsche"
   when "Outback" then "Subaru"
   when "520i" then "BMW"
   when "Tundra" then "Nissan"
   else "Unknown"

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

[edit] 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 where a number falls amongst a group of different ranges:

score = 70

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

puts result

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

[edit] 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.

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

Add to Cart

PreviousTable of ContentsNext
Ruby Flow ControlRuby While and Until Loops
Personal tools

Find us on Facebook