Introduction to the judgment Statement of Ruby Concise tutorials _ruby Topics

Source: Internet
Author: User
Tags case statement numeric logical operators modifier

Conditional judgments are present in the programming language, and the conditional judgments in Ruby are similar to those in Java, but there is a slight difference

Conditions in ruby conditional judgment:

1 can use comparison operations such as ==,<,> as a condition, comparison operations can return true and false, which is similar to the syntax in Java

2 Some other methods can also be used as a judgment condition, for example, empty? method, NULL to return True, otherwise return false

3 Some methods may not return true or false, but can also be used as conditions to determine the condition that they return the object, either false or nil, or the object of meaning, can be judged according to the following table:

TRUE FALSE
objects other than false and nil False and Nil


p/ruby/=~ "Ruby" returns 0, you can return true in conditional judgment

Common logical operators can also be used in Ruby, &&,| |,! , the meaning of the representation is consistent with the meaning in Java.

Conditional judgment statements in Ruby:

1. If statement

Copy Code code as follows:

=begin
Grammar:
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 exactly the opposite of the IF statement, when the condition does not match, then executes the corresponding statement

Copy Code code as follows:

=begin
Grammar:
Unless conditions then
Statement
End
=end

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

#-> ' A is smaller than B ' would be printed out.

3. Case statement
when the same object is to be compared to multiple values, you can use the case statement, which functions like a switch statement in Java

Copy Code code as follows:

=begin
Grammar:
The object that the case wants to compare
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
#这里比较的是对象的类型, not the value of an object

Ps:
The If modifier and the unless modifier can be written after the execution statement, for example, print "A is larger than B." If a>b, so Ruby is flexible.

The meaning of the symbol "= = =", which can represent different symbols on different occasions, if the left is a number or a string, it is the same as "= ="; In the case of a regular expression, it is equivalent to "=~"; in the case of class, to determine whether the object to the right of "= = =" is an instance of a class

Copy Code code as follows:

P ((1..3) = = 2) #-> true
p/zz/= = "Zyzzy" #-> 2
P String = = "Xyzzy" #-> True

#在case表达与if语句间转换, with = =, the left side of the symbol is the value of the case, the right is the variable
Case A
When value1 if value1 = = A
Statement 1 Statement 1
When value2 elsif value2 = = A
Statement 2 Statement 2
else Else
Statement 3 Statement 3
End 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.