In this chapter we will discuss more Ruby process control.
Case
We use case statements to test for ordered conditions. As we have seen, this is quite close to the switch of C,java, but more powerful.
ruby> i=8
ruby> case I
| at 1, 2..5
| print "1..5\n"
| when 6..10
| print "6..10\n"
| end< c7/>6..10
Nil
2..5 represents a range between 2 and 5. The following expression tests whether I is in the range:
(2..5) = = I
The case also uses the relational operator = = = To test several conditions at the same time. To keep Ruby's face to the object, = = = can be appropriately understood as the object appearing in the when condition. For example, the following code now tests whether strings are equal in the first time and then in the second when Expression matches.
ruby> case ' abcdef '
| When ' AAA ', ' BBB '
| print ' AAA or bbb\n '
| when/def/
| print ' includes/def/ \ n '
| end
includes/def/
Nil
while