Ruby Process Control Method _ruby topics

Source: Internet
Author: User
Tags goto regular expression
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
| When 1, 2..5
| Print "1..5\n"
| When 6..10
| Print "6..10\n"
| End
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. In order to maintain the nature of Ruby's face, = = = can be appropriately understood as the

Like. For example, the following code now tests whether the string is equal in the first time and then makes a regular expression match in the second.

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 very clearly, Ruby provides a useful way to build loops.

While is a duplicate if. We used it in guessing games and regular expressions (see previous chapters); Here, when the condition (condition) is true, it surrounds a code field to

A while condition...end the form loop. But 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 negate a test condition. Unless is the negation of if, until is a negative while. I'll leave them here for you to experiment.

There are four ways to interrupt the progress of a loop from inside. The 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:

There are four ways to interrupt loops from within. First, the break from the loop is completely exited from the cycle. Second, next jumps to the beginning of the next loop iteration (corresponding to the continue of C).

Three, Ruby has redo, it can restart the current iteration. The following is a demonstration of the meaning of Break,next,redo with C code:

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 approach is to jump out of the loop by Returen. The return result is not only jumping out of the loop, but also jumping out of the loop-containing method. If there is a parameter, it returns to the method call, or it returns nil.

For

C programmers will now want to know how to do a "for" loop. Ruby's for is a bit more interesting than you think. The following loop is run by the elements in the collection:

For ELT in collection
...
End


A collection can be a set of numbers (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 collection of other types, such as an array:

Ruby> for ELT in [100,-9.6, "Pickle"]
| Print "#{elt}\t (#{elt.type}) \ n"
| End
(Fixnum)
-9.6 (Float)
Pickle (String)
[ -9.6, "Pickle"]


But we went too far. For actually, it's another way of writing each, which happens to be our first example of an iterator. The following two forms are equivalent:

# If You ' re used to C or Java, might prefer.
For I in collection
...
End
# A Smalltalk programmer might prefer this.
Collection.each {|i|
...
}


Once you are familiar with iterators, it often replaces the traditional loops. They are generally easier to handle. So let's go on to learn more about iterators.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.