Changes

The Ruby case Statement

1 byte removed, 13:03, 1 December 2007
no edit summary
</table>
<hr>
 
 
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 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:
== 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: