![]() |
|
|
|
|
||||||||||||||||||||||||||||||||||
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 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.
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:
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 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.
forC programmers will be wondering by now how to make a "for"
loop. Ruby's for is a little more interesting than you
might expect. The loop below runs once for each element in the
collection:
for elt in collection ... end |
The collection can be a range of values (this is what most people mean when they talk about a for loop):
ruby> for num in (4..6)
| print num,"\n"
| end
4
5
6
4..6 |
It may also be some other kind of collection, 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"] |
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.
for i in collection
...
end
# A Smalltalk programmer might prefer this.
collection.each {|i|
...
} |
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.
|
Prev |
Contents |
Next |
|
© 2002-2004 Active-Venture.com Website Hosting Service |
|
| Register Domain Name - Cheap domain name registration and domain transfer service | Domain Name Registration - Domain registration and domain search service from $5.95 |
|
Disclaimer: This
documentation is provided only for the benefits of our website hosting customers.
|
|