Ruby: Case expression and rubycase expression

Source: Internet
Author: User

Ruby: Case expression and rubycase expression

There are two forms of Ruby case expressions:

The first form is similar to a set of continuous if statements: it allows you to list a set of conditions and execute the first statement corresponding to the true condition expression.

In the second form, specify a target at the top of the case statement, and each when clause lists one or more comparison conditions.

 

  • Like if, case returns the value of the last expression to be executed. if the expression and condition are in the same row, the then keyword can be used to differentiate them.

 


Ruby, a simple case when structure statement

% Is the remainder operator. case I % 15 refers to the remainder after dividing by 15.

Ruby judgment Scope

The answer upstairs is basically correct. I can explain it.
= ~ It is the matching symbol of the regular expression (regex) in ruby.
"A" = ~ [A] is true
"A" = ~ [B] is false
The left side is the comparison is not quantitative, and the right side is the regular expression. You can learn more.

What is the use of LS in sentences?
Unless gets = ~ /[A-z]/= unless gets contains lowercase letters of a-z

This method will check whether there are lowercase letters a-z in English in gets. If not, all caps will be written.

In addition to this method, there is another method:
A = "This is a good day! "
B = a. upcase
Puts "all upper case" if a = B

This method has two advantages.
1. easy to understand
2. ruby's upcase functions are supported in multiple languages, not only in English.

In addition, you have a syntax error in 'A' .. 'Z.
The general usage of in is for I in 0 .. 10, which is to assign a different value to the I loop for 10 times each time. If you know other languages, you should know that this is the syntax of the for statement. In this case, the statements are not case-insensitive and cannot be used.

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.