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.