Ruby 条件语句

Ruby 提供了现代语言常见的条件结构,在本章节中我们会学习到如何在 Ruby 中所有可用的条件语句使用方式。

1. if… 语句

if... 语句是条件语句中最基本的,当 if 后的条件表达式得到满足的时候,执行代码部分,不满足条件的时候,代码将被忽略。

实例:

if 10 < 20 then
    puts "10 is less than 20"
end

# ---- 输出结果 ----
10 is less than 20

解释:在执行时,代码将显示字符串“10 is less than 20”,因为 10 < 20 表达式的计算结果为trueend语句标志着if语句的结束。

经验:在实际开发中我们常常省略 then,得到的结果是相同的。

实例:

if 10 < 20
    puts "10 is less than 20"
end

# ---- 输出结果 ----
10 is less than 20

另外,我们还可以将 if 语句的判断部分后置,这叫做条件后置

实例:

puts "10 is less than 20" if 10 < 20

# ---- 输出结果 ----
10 is less than 20

if 后面的条件表达式可以使用逻辑运算符。

实例:

firstname = 'john'
lastname = "smith"

if firstname == "john" && lastname == "smith"
  puts "Hello John!"
end

# ---- 输出结果 ----
Hello John!

还有一种跟 if 相反的判断,名叫 unless,它和 if 刚好相反,当 unless 后面的条件不满足的时候,才执行代码部分。

实例:

unless 10 >= 20
    puts "10 not bigger than or equal 20"
end

# ---- 输出结果 ----
10 not bigger than or equal 20

注意事项:unless 也可以称为 if not,因为很容易让人混淆,所以我们尽量不使用它。

2. if…else语句

上面的 if 表达式只提供了条件表达式计算结果为 true,的情况,并没有为结果为 false 时进行考虑,if...else 语句在这时就起到了作用。

实例:

customerName = "Andrew"
if customerName == "Fred"
  puts "Hello Fred!"
else 
  puts "You're not Fred! Where's Fred?"
end

# ---- 输出结果 ----
You're not Fred! Where's Fred?

解释:当条件表达式为true的时候,执行else之前的内容,当表达式为false的时候,执行else之后的内容。

3. if…elsif…语句

当有许多条件需要判断的时候需要使用 if...elsif... 语句。

实例:

customerName = "Andrew"
if customerName == "Fred"
  puts "Hello Fred!"
elsif customerName == 'Andrew'
  puts "Hello Andrew!"
else 
  puts "You're not my customer."
end

# ---- 输出结果 ----
Hello Andrew!

我们也可以在某些情况省略 else

实例:

customerName = "Andrew"
if customerName == "Fred"
  puts "Hello Fred!"
elsif customerName == 'Andrew'
  puts "Hello Andrew!"
end

# ---- 输出结果 ----
Hello Andrew!

4. 三元运算符

三元运算符是一种便捷表达条件语句的形式,它的语法如下:

[条件] ? [true expression] : [false expression]

当条件为true的时候,会执行冒号左侧代表真的表达式,当条件为false的时候,会执行冒号右侧代表假的表达式。

实例:

customerName = "Andrew"
puts customerName == "Fred" ? "Hello Fred" : "Who are you?"

# ---- 输出结果 ----
Who are you?

5. case 表达式

当我们多次使用if...elsif...来检查值的时候,可以使用 case 表达式来替代。

实例:

比如使用 if..elsif...时表达式为这个亚子:

customerName = "John"
if customerName == "Fred"
  puts "Hello Fred!"
elsif customerName == "John"
  puts "Hello John!" 
elsif customername == "Robert"
  puts "Hello Bob!"
else
  puts "Who are you?"
end

# ---- 输出结果 ----
Hello John!

我们使用 case 表达式时:

customerName = "John"
case customerName
  when "Fred" then
    puts "Hello Fred!"
  when "John" then
    puts "Hello John!"
  when "Bobert" then
    puts "Hello Bob!"
  else
    puts "Who are you?"
end
    
# ---- 输出结果 ----
Hello John!

Tips : 当没找到匹配项,else 中定义的默认语句会执行。

注意事项:case 后面判断的变量可以是任意类型的,例如:字符串、数组、数字等。

case表达式在找到结果后可以把结果返回给变量。

car = "Patriot"

manufacturer = case car
   when "Focus" then "Ford"
   when "Navigator" then "Lincoln"
   when "Camry" then "Toyota"
   when "Civic" then "Honda"
   when "Patriot" then "Jeep"
   when "Jetta" then "VW"
   when "Ceyene" then "Porsche"
   when "Outback" then "Subaru"
   when "520i" then "BMW"
   when "Tundra" then "Nissan"
   else "Unknown"
end

puts "The " + car  + " is made by "  + manufacturer
     
# ---- 输出结果 ----
The Patriot is made by Jeep   

6. 小结

本章中我们学习了if...语句,if...else语句,if...elsif...语句,三元运算符以及case表达式。