Ruby 程式設計/語法/控制結構
Ruby 可以使用條件分支來控制程式碼的執行。條件分支根據測試表示式的結果來執行程式碼塊,具體取決於測試表達式為真還是假。如果測試表達式計算結果為常量 false 或 nil,則測試為假;否則為真。請注意,數字 zero 被認為是真,而許多其他程式語言認為它是假。
在許多流行的程式語言中,條件分支是語句。它們可以影響執行的程式碼,但它們本身不產生值。然而,在 Ruby 中,條件分支是表示式。它們與其他表示式一樣計算出值。例如,一個 if 表示式不僅決定子程式碼塊是否執行,而且它本身也會產生一個值。如果未執行任何程式碼塊,則 if 表示式將產生 nil。例如,以下 if 表示式計算結果為 3
if true 3 end
下面解釋了 Ruby 的條件分支。
示例
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 表示式與 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
當代碼塊中執行的程式碼只有一行時,“單行程式碼”非常方便。
elsif(注意它是 elsif 而不是 elseif)和 else 程式碼塊透過提供處理其他測試的選項來進一步控制您的指令碼。只有當 if 測試為假時才會考慮 elsif 和 else 程式碼塊。您可以有任意數量的 elsif 程式碼塊,但只能有一個 if 程式碼塊和一個 else 程式碼塊。
語法
if expression ...code block... elsif another expression ...code block... elsif another expression ...code block... else ...code block... end
“簡短 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)
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 Programming [1] 中看到 case 的其他用法及其語法的變體。
Ruby 中的 while 語句與 if 以及其他語言的 while(在語法上)非常相似
while <expression> <...code block...> end
只要表示式計算結果為 true,程式碼塊就會反覆執行。
此外,與 if 和 unless 一樣,以下操作也是可能的
<...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 會導致其出現的 method 在該點退出並返回指定的值。
請注意,如果 method 沒有 return 語句,則最後一個語句的值會隱式返回。