Introduction to judgment statements in Ruby concise tutorial

Source: Internet
Author: User
Tags case statement

Conditional judgment exists in programming languages. The conditional judgment in Ruby is similar to that in Java. Of course, there are still some differences.

Conditions for condition determination in Ruby:

1) Comparison operations such as =, <,> can be used as conditions. Comparison operations can return true and false, which is similar to the syntax in java.

2) some other methods can also be used as judgment conditions, such as empty? Method. If it is null, true is returned. Otherwise, false is returned.

3) Although some methods do not return true or false, they can also be used as conditions for condition judgment. The objects they return are either false or nil or meaningful objects, you can judge the problem according to the following table:

TRUE FALSE
Objects other than false and nil False and nil


P/Ruby/= ~ If "Ruby" returns 0, true can be returned in condition judgment.

Common logical operators can also be used in Ruby ,!, Its meaning is consistent with that in JAVA.

Condition judgment statement in Ruby:

1. if statement
Copy codeThe Code is as follows:
= Begin
Syntax:
If condition 1 then
Statement 1
Elsif condition 2 then
Statement 2
Elsif Condition 3 then
Statement 3
Else
Statement 4
End
= End

A = 10
B = 20
If a> B then
Print "a is smaller than B ."
Elsif a = B then
Print "a equals B ."
Else
Print "a is larger than B ."
End

2. unless statement, Which is the opposite of the if statement. When the conditions do not match, execute the corresponding statement.

Copy codeThe Code is as follows:
= Begin
Syntax:
Unless condition then
Statement
End
= End

A = 10
B = 20
Unless a> B then
Print "a is smaller than B ."
End

#-> "A is smaller than B" will be printed out.

3. case statement
When the same object needs to be compared with multiple values, the case statement can be used. Its function is similar to the switch statement in JAVA.

Copy codeThe Code is as follows:
= Begin
Syntax:
Case object to be compared
When value 1 then
Statement 1
When value 2 then
Statement 2
When value 3 then
Statement 3
Else
Statement 4
End
# Then can be omitted.
= End

Array = ["aa", 1, nil]
Item = array [0]
Case item
When String
Puts "item is a String ."
When Numeric
Puts "item is a Numeric ."
Else
Puts "item is a something"
End
# Here we compare the object type, not the object value.

PS:
The if modifier and unless modifier can be written after the execution statement. For example, print "a is larger than B." if a> B, ruby is flexible.

"=" Indicates the meaning of a symbol. It can represent different symbols in different occasions. If the left side is a number or string, it is the same as "=; in the case of a regular expression, it is equivalent to "= ~ "; In the case of a class, judge whether the object on the right is an instance of the class
Copy codeThe Code is as follows:
P (1 .. 3) = 2) #-> true
P/zz/= "zyhei" #-> 2
P String = "xyzzy" #-> true

# Convert between case expression and if statement. Use = to indicate the value of case on the left of the symbol and the variable of case on the right.
Case
When value1 if value1 =
Statement 1 Statement 1
When value2 elsif value2 =
Statement 2 Statement 2
Else
Statement 3 Statement 3
End

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.