Ruby 中的循環用于執行相同的代碼塊若干次。本章節將詳細介紹 Ruby 支持的所有循環語句。
while conditional [do] code end
或者
<pre> while conditional [:] code end
當 conditional 為真時,執行 code。
語法中 do 或 : 可以省略不寫。但若要在一行內寫出 while 式,則必須以 do 或 : 隔開條件式或程式區塊。
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 while $i < $num do puts("在循環語句中 i = #$i" ) $i +=1 end
以上實例輸出結果為:
Inside the loop i = 0 Inside the loop i = 1 Inside the loop i = 2 Inside the loop i = 3 Inside the loop i = 4
code while condition 或者 begin code end while conditional
當 conditional 為真時,執行 code。
如果 while 修飾符跟在一個沒有 rescue 或 ensure 子句的 begin 語句后面,code 會在 conditional 判斷之前執行一次。
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 begin puts("在循環語句中 i = #$i" ) $i +=1 end while $i < $num
以上實例輸出結果為:
在循環語句中 i = 0 在循環語句中 i = 1 在循環語句中 i = 2 在循環語句中 i = 3 在循環語句中 i = 4
until conditional [do] code end
當 conditional 為假時,執行 code。
語法中 do 可以省略不寫。但若要在一行內寫出 until 式,則必須以 do 隔開條件式或程式區塊。
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 until $i > $num do puts("在循環語句中 i = #$i" ) $i +=1; end
這將產生以下結果:
在循環語句中 i = 0 在循環語句中 i = 1 在循環語句中 i = 2 在循環語句中 i = 3 在循環語句中 i = 4 在循環語句中 i = 5
code until conditional 或者 begin code end until conditional
當 conditional 為假時,執行 code。
如果 until 修飾符跟在一個沒有 rescue 或 ensure 子句的 begin 語句后面,code 會在 conditional 判斷之前執行一次。
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 begin puts("在循環語句中 i = #$i" ) $i +=1; end until $i > $num
這將產生以下結果:
在循環語句中 i = 0 在循環語句中 i = 1 在循環語句中 i = 2 在循環語句中 i = 3 在循環語句中 i = 4 在循環語句中 i = 5
for variable [, variable ...] in expression [do] code end
針對 expression 中的每個元素分別執行一次 code。
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 puts "局部變量的值為 #{i}" end
在這里,我們已經定義了范圍 0..5。語句 for i in 0..5 允許 i 的值從 0 到 5(包含 5)。
嘗試一下 ?
以上實例輸出結果為:
局部變量的值為 0 局部變量的值為 1 局部變量的值為 2 局部變量的值為 3 局部變量的值為 4 局部變量的值為 5
for...in 循環幾乎是完全等價于:
(expression).each do |variable[, variable...]| code end
但是,for 循環不會為局部變量創建一個新的作用域。
語法中 do 可以省略不寫。但若要在一行內寫出 for 式,則必須以 do 隔開條件式或程式區塊。
#!/usr/bin/ruby # -*- coding: UTF-8 -*- (0..5).each do |i| puts "局部變量的值為 #{i}" end
以上實例輸出結果為:
局部變量的值為 0 局部變量的值為 1 局部變量的值為 2 局部變量的值為 3 局部變量的值為 4 局部變量的值為 5
break
終止最內部的循環。如果在塊內調用,則終止相關塊的方法(方法返回 nil)。
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i > 2 then break end puts "局部變量的值為 #{i}" end
以上實例輸出結果為:
局部變量的值為 0 局部變量的值為 1 局部變量的值為 2
next
跳到最內部循環的下一個迭代。如果在塊內調用,則終止塊的執行(yield 或調用返回 nil)。
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i < 2 then next end puts "局部變量的值為 #{i}" end
以上實例輸出結果為:
局部變量的值為 2 局部變量的值為 3 局部變量的值為 4 局部變量的值為 5
redo
重新開始最內部循環的該次迭代,不檢查循環條件。如果在塊內調用,則重新開始 yield 或 call。
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i < 2 then puts "局部變量的值為 #{i}" redo end end
這將產生以下結果,并會進入一個無限循環:
局部變量的值為 0 局部變量的值為 0 ............................
retry
如果 retry 出現在 begin 表達式的 rescue 子句中,則從 begin 主體的開頭重新開始。
begin do_something # 拋出的異常 rescue # 處理錯誤 retry # 重新從 begin 開始 end
如果 retry 出現在迭代內、塊內或者 for 表達式的主體內,則重新開始迭代調用。迭代的參數會重新評估。
for i in 1..5 retry if some_condition # 重新從 i == 1 開始 end
#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 1..5 retry if i > 2 puts "局部變量的值為 #{i}" end
這將產生以下結果,并會進入一個無限循環:
局部變量的值為 1 局部變量的值為 2 局部變量的值為 1 局部變量的值為 2 局部變量的值為 1 局部變量的值為 2 ............................