Ruby/Struktury sterujące: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Kj (dyskusja | edycje)
m +nav
Kj (dyskusja | edycje)
m <pre>
Linia 4:
We use the case statement to test a sequence of conditions. This is superficially similar to switch in C and Java but is considerably more powerful, as we shall see.
 
<pre>
ruby> i=8
ruby> case i
Linia 14 ⟶ 15:
nil
 
</pre>
2..5 is an expression which means the range between 2 and 5, inclusive. The following expression tests whether the value of i falls within that range:
 
<pre>
(2..5) === i
</pre>
 
case internally uses the relationship operator === to check for several conditions at a time. In keeping with ruby's object oriented nature, === is interpreted suitably for the object that appeared in the when condition. For example, the following code tests string equality in the first when, and regular expression matching in the second when.
 
<pre>
ruby> case 'abcdef'
| when 'aaa', 'bbb'
Linia 26 ⟶ 31:
| puts "includes /def/"
| end
</pre>
<pre>
includes /def/
nil
while
</pre>
 
Ruby provides convenient ways to construct loops, although you will find in the next chapter that learning how to use iterators will make it unnecessary to write explicit loops very often.
Linia 34 ⟶ 42:
A while is a repeated if. We used it in our word-guessing puzzle and in the regular expression programs (see the previous chapter); there, it took the form while condition ... end surrounding a block of code to be repeated while condition was true. But while and if can as easily be applied to individual statements:
 
<pre>
ruby> i = 0
0
Linia 42 ⟶ 51:
nil
ruby> puts i+=1 while i<3
</pre>
<pre>
1
2
3
nil
</pre>
 
Sometimes you want to negate a test condition. An unless is a negated if, and an until is a negated while. We'll leave it up to you to experiment with these.
Linia 51 ⟶ 63:
There are four ways to interrupt the progress of a loop from inside. First, break means, as in C, to escape from the 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 illustrating the meanings of break, next, and redo:
 
<pre>
while (condition) {
label_redo:
Linia 62 ⟶ 75:
label_break:
...
</pre>
 
The fourth way to get out of a loop from the inside is return. An evaluation of return causes escape not only from a loop but from the method that contains the loop. If an argument is given, it will be returned from the method call, otherwise nil is returned.
Linia 68 ⟶ 82:
C programmers will be wondering by now how to make a "for" loop. Ruby's for can serve the same purpose, but adds some flexibility. The loop below runs once for each element in a collection (array, hash, numeric sequence, etc.), but doesn't make the programmer think about indices:
 
<pre>
for elt in collection
# ... here, elt refers to an element of the collection
end
</pre>
 
The collection can be a range of values (this is what most people mean when they talk about a for loop):
 
<pre>
ruby> for num in (4..6)
| puts num
Linia 81 ⟶ 98:
6
4..6
</pre>
 
In this example we step through some array elements:
 
<pre>
ruby> for elt in [100,-9.6,"pickle"]
| puts "#{elt}\t(#{elt.class})"
Linia 91 ⟶ 110:
pickle (String)
[100, -9.6, "pickle"]
</pre>
 
But we're getting ahead of ourselves. for is really another way of writing each, which, it so happens, is our first example of an iterator. The following two forms are equivalent:
 
# If you're used to C or Java, you might prefer this.
 
<pre>
for element in collection
...
end
</pre>
 
# A Smalltalk programmer might prefer this.
 
<pre>
collection.each {|element|
...
}
</pre>
 
Iterators can often be substituted for conventional loops, and once you get used to them, they are generally easier to deal with. So let's move on and learn more about them.