Ruby 中的循環(huán)用于執(zhí)行相同的代碼塊若干次。本章節(jié)將詳細介紹 Ruby 支持的所有循環(huán)語句。
Ruby while 語句
語法
while conditional [do] code end
當(dāng) conditional 為真時,執(zhí)行 code。while 循環(huán)的 conditional 通過保留字 do、一個換行符、反斜線 \ 或一個分號 ; ,來與 code 分離開。
實例
#!/usr/bin/ruby $i = 0 $num = 5 while $i $num do puts("Inside the loop i = #$i" ) $i +=1 end
這將產(chǎn)生以下結(jié)果:
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
Ruby while 修飾符
語法
code while condition OR begin code end while conditional
當(dāng) conditional 為真時,執(zhí)行 code。
如果 while 修飾符跟在一個沒有 rescue 或 ensure 子句的 begin 語句后面,code 會在 conditional 判斷之前執(zhí)行一次。
實例
#!/usr/bin/ruby $i = 0 $num = 5 begin puts("Inside the loop i = #$i" ) $i +=1 end while $i $num
這將產(chǎn)生以下結(jié)果:
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 Ruby until 語句 until conditional [do] code end
當(dāng) conditional 為假時,執(zhí)行 code。until 語句的 conditional 通過保留字 do、一個換行符或一個分號,來與 code 分離開。
實例
#!/usr/bin/ruby $i = 0 $num = 5 until $i > $num do puts("Inside the loop i = #$i" ) $i +=1; end
這將產(chǎn)生以下結(jié)果:
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 Inside the loop i = 5 Ruby until 修飾符 語法 code until conditional OR begin code end until conditional
當(dāng) conditional 為假時,執(zhí)行 code。
如果 until 修飾符跟在一個沒有 rescue 或 ensure 子句的 begin 語句后面,code 會在 conditional 判斷之前執(zhí)行一次。
實例
#!/usr/bin/ruby $i = 0 $num = 5 begin puts("Inside the loop i = #$i" ) $i +=1; end until $i > $num
這將產(chǎn)生以下結(jié)果:
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 Inside the loop i = 5
Ruby for 語句
語法
for variable [, variable ...] in expression [do] code end
針對 expression 中的每個元素分別執(zhí)行一次 code。
實例
#!/usr/bin/ruby for i in 0..5 puts "Value of local variable is #{i}" end
在這里,我們已經(jīng)定義了范圍 0..5。語句 for i in 0..5 允許 i 的值從 0 到 5(包含 5)。這將產(chǎn)生以下結(jié)果:
Value of local variable is 0 Value of local variable is 1 Value of local variable is 2 Value of local variable is 3 Value of local variable is 4 Value of local variable is 5
for...in 循環(huán)幾乎是完全等價于:
(expression).each do |variable[, variable...]| code end
但是,for 循環(huán)不會為局部變量創(chuàng)建一個新的作用域。for 循環(huán)的 expression 通過保留字 do、一個換行符或一個分號,來與 code 分離開。.
實例
#!/usr/bin/ruby (0..5).each do |i| puts "Value of local variable is #{i}" end
這將產(chǎn)生以下結(jié)果:
Value of local variable is 0 Value of local variable is 1 Value of local variable is 2 Value of local variable is 3 Value of local variable is 4 Value of local variable is 5
Ruby break 語句
語法
break
終止最內(nèi)部的循環(huán)。如果在塊內(nèi)調(diào)用,則終止相關(guān)塊的方法(方法返回 nil)。
實例
#!/usr/bin/ruby for i in 0..5 if i > 2 then break end puts "Value of local variable is #{i}" end
這將產(chǎn)生以下結(jié)果:
Value of local variable is 0 Value of local variable is 1 Value of local variable is 2
Ruby next 語句
語法
next
跳到最內(nèi)部循環(huán)的下一個迭代。如果在塊內(nèi)調(diào)用,則終止塊的執(zhí)行(yield 或調(diào)用返回 nil)。
實例
#!/usr/bin/ruby for i in 0..5 if i 2 then next end puts "Value of local variable is #{i}" end
這將產(chǎn)生以下結(jié)果:
Value of local variable is 2 Value of local variable is 3 Value of local variable is 4 Value of local variable is 5
Ruby redo 語句
語法
redo
重新開始最內(nèi)部循環(huán)的該次迭代,不檢查循環(huán)條件。如果在塊內(nèi)調(diào)用,則重新開始 yield 或 call。
實例
#!/usr/bin/ruby for i in 0..5 if i 2 then puts "Value of local variable is #{i}" redo end end
這將產(chǎn)生以下結(jié)果,并會進入一個無限循環(huán):
Value of local variable is 0 Value of local variable is 0 ............................
Ruby retry 語句
語法
retry
如果 retry 出現(xiàn)在 begin 表達式的 rescue 子句中,則從 begin 主體的開頭重新開始。
begin do_something # 拋出的異常 rescue # 處理錯誤 retry # 重新從 begin 開始 end
如果 retry 出現(xiàn)在迭代內(nèi)、塊內(nèi)或者 for 表達式的主體內(nèi),則重新開始迭代調(diào)用。迭代的參數(shù)會重新評估。
for i in 1..5 retry if some_condition # 重新從 i == 1 開始 end
實例
#!/usr/bin/ruby for i in 1..5 retry if i > 2 puts "Value of local variable is #{i}" end
這將產(chǎn)生以下結(jié)果,并會進入一個無限循環(huán):
Value of local variable is 1 Value of local variable is 2 Value of local variable is 1 Value of local variable is 2 Value of local variable is 1 Value of local variable is 2 ............................
標(biāo)簽:內(nèi)江 河池 南昌 克拉瑪依 西雙版納 甘孜 大慶 棗莊
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解Ruby中的循環(huán)語句的用法》,本文關(guān)鍵詞 詳解,Ruby,中的,循環(huán),語句,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。