跳轉至內容

Ruby 程式設計/語法/控制結構

來自華夏公益教科書,開放的書本,開放的世界

控制結構

[編輯 | 編輯原始碼]

條件分支

[編輯 | 編輯原始碼]

Ruby 可以使用條件分支控制程式碼的執行。條件分支根據測試 表示式 的結果,執行程式碼塊,取決於測試表達式是真還是假。如果測試表達式計算結果為常量 falsenil,則測試為假;否則,測試為真。注意,數字 被認為是真,而許多其他程式語言認為它是假。

在許多流行的程式語言中,條件分支是語句。它們可以影響執行哪段程式碼,但它們本身不會產生值。然而,在 Ruby 中,條件分支是表示式。它們會像其他表示式一樣計算出值。例如,一個 if 表示式不僅會確定是否執行子程式碼塊,還會產生它自己的值。如果未執行任何程式碼塊,則 if 表示式將產生 nil。例如,以下 if 表示式計算結果為 3

 if true
   3
 end

Ruby 的條件分支在下面解釋。

if 表示式

[編輯 | 編輯原始碼]

示例

 a = 5
 if a == 4
   a = 7
 end
 print a # prints 5 since the if-block isn't executed

如果您使用 then,也可以將測試表達式和程式碼塊放在同一行

 if a == 4 then a = 7 end
 #or
 if a == 4: a = 7 end
 #Note that the ":" syntax for if one line blocks do not work anymore in ruby 1.9. Ternary statements still work

這等同於

 a = 5
 a = 7 if a == 4
 print a # prints 5 since the if-block isn't executed

unless 表示式

[編輯 | 編輯原始碼]

unless-表示式與 if-表示式相反,它包含的程式碼塊只有在測試表達式為假時才會執行。

示例

 a = 5
 unless a == 4
   a = 7
 end
 print a # prints 7 since the unless-block is executed

unless 表示式幾乎完全等同於一個取反的 if 表示式

 if !expression # is equal to using unless expression

區別在於 unless 不允許後跟 elsif。並且沒有 elsunless

與 if-表示式一樣,您也可以編寫

 a = 5
 a = 7 unless a == 4
 print a # prints 7 since the unless-block is executed

當代碼塊中執行的程式碼只有一行時,“單行程式碼”非常方便。

if-elsif-else 表示式

[編輯 | 編輯原始碼]

elsif (請注意它是 elsif 而不是 elseif) 和 else 程式碼塊透過提供容納其他測試的選項,讓您更有效地控制指令碼。只有在 if 測試為假時,才會考慮 elsifelse 程式碼塊。您可以包含任意數量的 elsif 程式碼塊,但只有一個 if 程式碼塊和一個 else 程式碼塊。

語法

 if expression
   ...code block...
 elsif another expression
   ...code block...
 elsif another expression
   ...code block...
 else
   ...code block...
 end

短路 if 表示式(也稱為三元運算子)

[編輯 | 編輯原始碼]

“短路 if”語句為您提供了一種節省空間的方法來評估表示式並返回一個值。

格式為

 result = (condition) ? (expression-if-true) : (expression-if-false)

它也被稱為三元運算子,建議僅將此語法用於次要任務,例如字串格式化,因為這可能會導致程式碼可讀性差。

 irb(main):037:0> true ? 't' : 'f'
 => "t"
 irb(main):038:0> false ? 't' : 'f'
 => "f"

這在進行字串連線等操作時非常有用。

示例

 a = 5
 plus_or_minus = '+'
 print "The number #{a}#{plus_or_minus}1 is: #{plus_or_minus == '+' ? (a+1).to_s : (a-1).to_s}."

此外,這也可以寫成

 result = if condition then expression-1 else expression-2 end

賦值可以寫成

 result = (value-1 if expression-1) || (value-2 if expression-2)

case 表示式

[編輯 | 編輯原始碼]

if-elsif-else 表示式(上面)的替代方法是 case 表示式。Ruby 中的 case 支援多種語法。例如,假設我們想確定一個數字(由變數 a 給出)與 5 之間的關係。我們可以說

 a = 1
 case 
 when a < 5 then puts "#{a} is less than 5"    
 when a == 5 then puts "#{a} equals 5"   
 when a > 5 then puts "#{a} is greater than 5" 
 end

注意,與 if 一樣,比較運算子是 ==。賦值運算子是 =。雖然 Ruby 會接受賦值運算子

   when a = 5 then puts "#{a} equals 5"   # WARNING! This code CHANGES the value of a!

但這不是我們想要的!在這裡,我們想要比較運算子。

case 的更簡潔語法是隱含比較

 case a
 when 0..4 then puts "#{a} is less than 5"    
 when 5 then puts "#{a} equals 5" 
 when 5..10 then puts "#{a} is greater than 5" 
 else puts "unexpected value #{a} "         # Just in case "a" is bigger than 10 or negative.
 end

注意:因為範圍是明確說明的,所以處理 a 的意外值是一個良好的編碼實踐。這種簡潔的語法在我們預先知道要期望的值時可能最有用。例如

 a = "apple"
 case a
 when "vanilla" then "a spice"    
 when  "spinach" then "a vegetable" 
 when "apple" then "a fruit" 
 else "an unexpected value"
 end

如果輸入到 IRB,這將給出

 => "a fruit"

在 Linuxtopia Ruby 程式設計 [1] 中,可以找到使用 case 及其語法變化的其他方法。

Ruby 中的 while 語句與 if 以及其他語言的 while(語法上)非常相似

while <expression>
  <...code block...>
end

程式碼塊將一次又一次地執行,只要表示式計算結果為 true

此外,與 ifunless 一樣,以下操作也是可能的

<...code...> while <expression>

請注意以下奇怪的情況有效...

 line = readline.chomp while line != "what I'm looking for"

因此,如果區域性變數 line 在此行之前不存在,則在第一次看到它時,它在迴圈表示式第一次計算時具有值 nil

until 語句在功能上類似於 while 語句。與 while 語句不同,until 迴圈的程式碼塊將一直執行,直到表示式計算結果為 false

until <expression>
  <...code block...>
end

關鍵字

[編輯 | 編輯原始碼]

return value 會導致包含它的方法在該點退出並返回指定的值。

請注意,如果一個方法沒有 return 語句,則隱式返回最後一條語句的值。

華夏公益教科書