詳解Ruby中的循環(huán)語句的用法
Ruby 中的循環(huán)用于執(zhí)行相同的代碼塊若干次。本章節(jié)將詳細(xì)介紹 Ruby 支持的所有循環(huán)語句。
Ruby while 語句
語法
while conditional [do] code end
當(dāng) conditional 為真時(shí),執(zhí)行 code。while 循環(huán)的 conditional 通過保留字 do、一個(gè)換行符、反斜線 \ 或一個(gè)分號 ; ,來與 code 分離開。
實(shí)例
#!/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 為真時(shí),執(zhí)行 code。
如果 while 修飾符跟在一個(gè)沒有 rescue 或 ensure 子句的 begin 語句后面,code 會(huì)在 conditional 判斷之前執(zhí)行一次。
實(shí)例
#!/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 為假時(shí),執(zhí)行 code。until 語句的 conditional 通過保留字 do、一個(gè)換行符或一個(gè)分號,來與 code 分離開。
實(shí)例
#!/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 為假時(shí),執(zhí)行 code。
如果 until 修飾符跟在一個(gè)沒有 rescue 或 ensure 子句的 begin 語句后面,code 會(huì)在 conditional 判斷之前執(zhí)行一次。
實(shí)例
#!/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 中的每個(gè)元素分別執(zhí)行一次 code。
實(shí)例
#!/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)幾乎是完全等價(jià)于:
(expression).each do |variable[, variable...]| code end
但是,for 循環(huán)不會(huì)為局部變量創(chuàng)建一個(gè)新的作用域。for 循環(huán)的 expression 通過保留字 do、一個(gè)換行符或一個(gè)分號,來與 code 分離開。.
實(shí)例
#!/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)。
實(shí)例
#!/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)的下一個(gè)迭代。如果在塊內(nèi)調(diào)用,則終止塊的執(zhí)行(yield 或調(diào)用返回 nil)。
實(shí)例
#!/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。
實(shí)例
#!/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ì)進(jìn)入一個(gè)無限循環(huán):
Value of local variable is 0 Value of local variable is 0 ............................
Ruby retry 語句
語法
retry
如果 retry 出現(xiàn)在 begin 表達(dá)式的 rescue 子句中,則從 begin 主體的開頭重新開始。
begin do_something # 拋出的異常 rescue # 處理錯(cuò)誤 retry # 重新從 begin 開始 end
如果 retry 出現(xiàn)在迭代內(nèi)、塊內(nèi)或者 for 表達(dá)式的主體內(nèi),則重新開始迭代調(diào)用。迭代的參數(shù)會(huì)重新評估。
for i in 1..5 retry if some_condition # 重新從 i == 1 開始 end
實(shí)例
#!/usr/bin/ruby
for i in 1..5
retry if i > 2
puts "Value of local variable is #{i}"
end
這將產(chǎn)生以下結(jié)果,并會(huì)進(jìn)入一個(gè)無限循環(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 ............................
相關(guān)文章
Ruby中調(diào)用執(zhí)行shell命令的6種方法
這篇文章主要介紹了Ruby中調(diào)用執(zhí)行shell命令的6種方法,本文羅列了Ruby中可以調(diào)用和執(zhí)行Linux系統(tǒng)Shell命令的6個(gè)方法,需要的朋友可以參考下2014-10-10
Ruby和Ruby on Rails中解析JSON格式數(shù)據(jù)的實(shí)例教程
這篇文章主要介紹了Ruby和Ruby on Rails中解析JSON格式數(shù)據(jù)的實(shí)例教程,文中介紹到的Ruby on Rails中的JSON解析工具包json_pure使用起來十分方便,需要的朋友可以參考下2016-04-04
實(shí)例講解Ruby中的鉤子方法及對方法調(diào)用添加鉤子
鉤子方法即是在普通的方法上添加"鉤子",使特定事件發(fā)生時(shí)可以被調(diào)用,下面就來以實(shí)例講解Ruby中的鉤子方法及對方法調(diào)用添加鉤子2016-06-06
Ruby實(shí)現(xiàn)生產(chǎn)者和消費(fèi)者代碼分享
這篇文章主要介紹了Ruby實(shí)現(xiàn)生產(chǎn)者和消費(fèi)者代碼分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-05-05
Ruby實(shí)現(xiàn)插入排序算法及進(jìn)階的二路插入排序代碼示例
插入排序即是把已有的有序序列從后向前掃描插入元素,數(shù)值大的向后移動(dòng),這里我們就來看一下使用Ruby實(shí)現(xiàn)插入排序算法及進(jìn)階的二路插入排序代碼示例2016-06-06
win7安裝ruby on rails開發(fā)環(huán)境
看到很多文章都說ruby環(huán)境在windows上是非常難搭建,會(huì)出現(xiàn)各種各樣的怪問題,所以都推薦到linux和mac上安裝開發(fā)。但是我按照教程搭了下,問題也不算太多??傔^大概花費(fèi)了2個(gè)半小時(shí)左右就完成了。下面就把安裝的步驟及具體的版本記錄了一下供大家參考。2014-07-07
Ruby升級后no such file to load -- readline解決辦法
這篇文章主要介紹了Ruby升級后no such file to load -- readline解決辦法,需要的朋友可以參考下2015-04-04
使用Ruby來編寫訪問Twitter的命令行應(yīng)用程序的教程
這篇文章主要介紹了使用Ruby來編寫訪問Twitter的命令行應(yīng)用程序的教程,文章來自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下2015-04-04

