In this chapter, we will discuss more Ruby process control.
Case
We use case statements to test ordered conditions. As we have seen, this is close to the switch in C and Java, but it is more powerful.
Ruby> I = 8
Ruby> case I
| When 1, 2 .. 5
| Print "1 .. 5 \ n"
| When 6 .. 10
| Print "6 .. 10 \ n"
| End
6 .. 10
Nil
2. 5 indicates a range between 2 and 5. The following expression tests whether I is in the range:
(2. 5) = I
In case, Relational operators ===are also used to test several conditions at the same time. To maintain ruby's object-oriented nature, ==== can be properly understood as a pair that appears in the when condition.
For example, the following code now tests whether the strings are equal in the first when and matches the regular expression in the second when.
Ruby> case 'abcdef'
| When 'aaa', 'bbb'
| Print "aaa or bbb \ n"
| When/def/
| Print "includes/def/\ n"
| End
Includes/def/
Nil
While
Although you will find in the next chapter that you do not need to write the loop body clearly, Ruby provides a set of useful methods to build a loop.
While is a duplicate if. we have used it in predicate games and regular expressions (see the previous chapter). Here, when condition is true, it uses
While condition... end form loop. However, while and if can be easily applied to separate statements:
Ruby> I = 0
0
Ruby> print "It's zero. \ n" if I = 0
It's zero.
Nil
Ruby> print "It's negative. \ n" if I <0
Nil
Ruby> print "# {I + = 1} \ n" while I <3
1
2
3
Nil
Sometimes you want to deny a test condition. unless is the rejection of if, while until is a negation. Here I will leave them for your experiment.
There are four ways to interrupt the progress of a loop from inside. First, break means, as in C, to escape from
Loop entirely. Second, next skips to the beginning of the next iteration of the loop (corresponding to C's continue ).
Third, ruby has redo, which restarts the current iteration. The following is C code specified strating the meanings of break,
Next, and redo:
There are four methods to interrupt the loop from the internal. first, like C, break completely exits from the loop. second, next jumps to the start of the next iteration (corresponding to the continue of C ). the
3. Ruby has redo, which can re-start the current iteration. The following uses C code to demonstrate the meaning of break, next, and redo:
While (condition ){
Label_redo:
Goto label_next;/* ruby's "next "*/
Goto label_break;/* ruby's "break "*/
Goto label_redo;/* ruby's "redo "*/
...
...
Label_next:
}
Label_break:
...
The fourth method is to returen. the return result is not only exceeded from the loop, but also exceeded from the method containing the loop. if there is a parameter, it will return to the method call, otherwise it will return nil.
For
C programmers now want to know how to create a "for" loop. Ruby's for is a little more interesting than you think. The following loop is controlled by the elements in the Set:
For elt in collection
...
End
A set can be a number set (also a for loop in the traditional sense ):
Ruby> for num in (4 .. 6)
| Print num, "\ n"
| End
4
5
6
4. 6
It can also be a set of other types, such as an array:
Ruby> for elt in [100,-9.6, "pickle"]
| Print "# {elt} \ t (# {elt. type}) \ n"
| End
100 (Fixnum)
-9.6 (Float)
Pickle (String)
[100,-9.6, "pickle"]
However, we have mentioned the header. for is actually another method of each. Coincidentally, this is our first example of iterator. The following two forms are equivalent:
# If you're used to C or Java, you might prefer this.
For I in collection
...
End
# A Smalltalk programmer might prefer this.
Collection. each {| I |
...
}
Once you are familiar with the iterator, it will often replace the traditional loop. They are generally easier to process. Therefore, let's continue to learn