1. ifexpress
1.1 格式:ruby
if conditional [then] code... [elsif conditional [then] code...]... [else code...] end
if 表達式用於條件執行。值 false 和 nil 爲假,其餘值都爲真。請注意,Ruby 使用 elsif,不是使用 else if 和 elif。less
若是 conditional 爲真,則執行 code。若是 conditional 不爲真,則執行 else 子句中指定的 code。code
一般咱們省略保留字 then 。若想在一行內寫出完整的 if 式,則必須以 then 隔開條件式和程式區塊。it
1.2 if修飾符io
格式class
code if condition
if修飾詞組表示當 if 右邊之條件成立時才執行 if 左邊的式子。即若是 conditional 爲真,則執行 code。循環
1.3 unless語句yield
格式:語法
unless conditional [then] code else code end
unless式和 if式做用相反,即若是 conditional 爲假,則執行 code。若是 conditional 爲真,則執行 else 子句中指定的 code。
1.4 unless修飾符
code unless conditional
若是 conditional 爲假,則執行 code。
1.5 case語句
case expression [when expression [, expression ...] [then] code ]... [else code ] end
case先對一個 expression 進行匹配判斷,而後根據匹配結果進行分支選擇。
它使用 ===運算符比較 when 指定的 expression,若一致的話就執行 when 部分的內容。
一般咱們省略保留字 then 。若想在一行內寫出完整的 when 式,則必須以 then 隔開條件式和程式區塊。
2.循環
2.1 while do
while conditional [do] #或者[:] code end
當 conditional 爲真時,執行 code。
語法中 do 或 : 能夠省略不寫。但若要在一行內寫出 while 式,則必須以 do 或 : 隔開條件式或程式區塊。
2.2 while修飾符 至關於 do while
code while condition 或者 begin code end while conditional
當 conditional 爲真時,執行 code。
若是 while 修飾符跟在一個沒有 rescue 或 ensure 子句的 begin 語句後面,code 會在 conditional 判斷以前執行一次。
2.3 until 語句
until conditional [do] code end
當 conditional 爲假時,執行 code。
語法中 do 能夠省略不寫。但若要在一行內寫出 until 式,則必須以 do 隔開條件式或程式區塊。
2.4 until修飾循環
code until conditional 或者 begin code end until conditional
當 conditional 爲 false 時,執行 code。
若是 until 修飾符跟在一個沒有 rescue 或 ensure 子句的 begin 語句後面,code 會在 conditional 判斷以前執行一次。
3. for 語句
for variable [, variable ...] in expression [do] code end
for...in 循環幾乎是徹底等價於:
(expression).each do |variable[, variable...]| code end
4.break 終止最內部的循環。若是在塊內調用,則終止相關塊的方法(方法返回 nil)。
5. next 相似於c的continue
6.redo 從新開始最內部循環的該次迭代,不檢查循環條件。若是在塊內調用,則從新開始 yield 或 call。
7. retry(1.9以後不支持)
若是 retry 出如今 begin 表達式的 rescue 子句中,則從 begin 主體的開頭從新開始。
若是 retry 出如今迭代內、塊內或者 for 表達式的主體內,則從新開始迭代調用。迭代的參數會從新評估。