Ruby中的循環用于執行相同的代碼塊指定的次數。本章將詳細介紹Ruby支持的循環語句。
Ruby while 語句:
語法:
while conditional [do]
code
end
執行代碼當條件為true時。while循環的條件是代碼中的保留字,換行,反斜杠(/)或一個分號隔開。
實例:
#!/usr/bin/ruby$i = 0$num = 5while $i < $num do puts("Inside the loop i = #$i" ) $i +=1end
這將產生以下結果:
Inside the loop i = 0Inside the loop i = 1Inside the loop i = 2Inside the loop i = 3Inside the loop i = 4
Ruby while 修辭符:
語法:
code while condition
OR
begin
code
end while conditional
執行代碼,當條件為true。
如果while 修飾符緊跟一個begin 語句但是沒有 rescue 或 ensure 子句, 代碼被執行前一次條件求值。
實例:
#!/usr/bin/ruby$i = 0$num = 5begin puts("Inside the loop i = #$i" ) $i +=1end while $i < $num
這將產生以下結果:
Inside the loop i = 0Inside the loop i = 1Inside the loop i = 2Inside the loop i = 3Inside the loop i = 4
Ruby until 語句:
until conditional [do]
code
end
執行代碼當條件為false。until 條件語句從代碼分離的保留字,換行符或分號。
語句:
#!/usr/bin/ruby$i = 0$num = 5until $i > $num do puts("Inside the loop i = #$i" ) $i +=1;end
這將產生以下結果:
Inside the loop i = 0Inside the loop i = 1Inside the loop i = 2Inside the loop i = 3Inside the loop i = 4Inside the loop i = 5
Ruby until 修辭符:
語法:
code until conditional
OR
begin
code
end until conditional
執行代碼當條件為 false。
如果 until 修辭符跟著 begin 語句但沒有 rescue 或 ensure 子句, 代碼一旦被執行在條件求值之前。
例子:
#!/usr/bin/ruby$i = 0$num = 5begin puts("Inside the loop i = #$i" ) $i +=1;end until $i > $num
這將產生以下結果:
Inside the loop i = 0Inside the loop i = 1Inside the loop i = 2Inside the loop i = 3Inside the loop i = 4Inside the loop i = 5
Ruby for 語句:
語法:
for variable [, variable ...] in expression [do]
code
end
一次執行代碼的每個元素在 in 表達式。
實例:
#!/usr/bin/rubyfor i in 0..5 puts "Value of local variable is #{i}"end
這里我們定義的范圍 0 .. 5 。因為在語句 for i in 0..5 將允許取值的范圍從0到5(含5),這將產生以下結果:
Value of local variable is 0Value of local variable is 1Value of local variable is 2Value of local variable is 3Value of local variable is 4Value of local variable is 5
for...in 循環幾乎是完全等同于:
(expression).each do |variable[, variable...]| code end
除了一個for循環不創建一個新的局部變量的范圍。一個循環的表情從代碼分離,保留字,一個換行符,或分號。
例子:
#!/usr/bin/ruby(0..5).each do |i| puts "Value of local variable is #{i}"end
這將產生以下結果:
Value of local variable is 0Value of local variable is 1Value of local variable is 2Value of local variable is 3Value of local variable is 4Value of local variable is 5
Ruby break 語句:
語法:
break
終止大多數內部的循環。終止塊內的方法返回nil如果調用的方法與相關塊。
實例:
#!/usr/bin/rubyfor i in 0..5 if i > 2 then break end puts "Value of local variable is #{i}"end
這將產生以下結果:
Value of local variable is 0Value of local variable is 1Value of local variable is 2
Ruby next 語句:
語法:
next
跳轉到最內部循環的下一次迭代。如果調用塊一個塊內終止執行(帶 yield 或調用返回 nil )。
例子:
#!/usr/bin/rubyfor i in 0..5 if i < 2 then next end puts "Value of local variable is #{i}"end
這將產生以下結果:
Value of local variable is 2Value of local variable is 3Value of local variable is 4Value of local variable is 5
Ruby redo 語句:
語法:
redo
會重新啟動啟動這個最內部的循環迭代,而不檢查循環條件。
會重新啟動 yield or call ,如果一個塊內調用。
例子:
#!/usr/bin/rubyfor i in 0..5 if i < 2 then puts "Value of local variable is #{i}" redo endend
這將產生以下結果,將執行無限循環:
Value of local variable is 0Value of local variable is 0............................
Ruby retry 語句:
語法:
retry
如果 retry 表達出現在 rescue 子句,則從開始重新開始。
begin do_something # exception raisedrescue # handles error retry # restart from beginningend
如果出現重試迭代,塊,或體內的表達,重新啟動迭代調用。迭代器的參數條件將重新計算。
for i in 1..5 retry if some_condition # restart from i == 1end
實例:
#!/usr/bin/rubyfor i in 1..5 retry if i > 2 puts "Value of local variable is #{i}"end
這將產生以下結果,將進入無限循環:
Value of local variable is 1Value of local variable is 2Value of local variable is 1Value of local variable is 2Value of local variable is 1Value of local variable is 2............................
新聞熱點
疑難解答
圖片精選