1.4 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	
			1.4 KiB
		
	
	
	
	
	
	
	
title, localeTitle
| title | localeTitle | 
|---|---|
| Ruby Conditionals | Ruby条件 | 
Ruby有几个常用的条件。
如果声明
在许多编程语言中非常常见的条件,语句测试条件是否为真,然后分支到指定的操作。 if语句包含一个if , 任何数量的elsif和至多一个else声明。
- 
fruit = :apple if fruit == :apple puts "Your fruit is an apple" elsif fruit == :orange puts "Your fruit is an orange" else puts "This is not an apple or an orange" end
除非声明
除非语句与if语句相反。它与否定的if语句相同。
- ruby happy = true if !happy puts "This person is not happy" end以上陈述等同于以下陈述
- ruby unless happy puts "This person is not happy" end
句尾条件
在ruby当里我们也常将条件至于句尾
- puts "Your fruit is an apple" if fruit == :apple
- puts "This is not an apple" unless fruit == :apple
三元声明
三元语句用作短条件语句。它写成如下
- ruby game = "won" fans = game == "won" ? "happy" : unhappy fans # => "happy"
案例陈述
case语句类似于if / elsif / else语句
- 
fruit = :apple case fruit when :apple puts "Your fruit is an apple" when :orange puts "Your fruit is an orange" else puts "This is not an apple or an orange" end