Ruby's Process Control approach

Source: Internet
Author: User

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

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.