Ruby - 迴圈



Ruby 中的迴圈用於執行相同的程式碼塊指定次數。本章詳細介紹了 Ruby 支援的所有迴圈語句。

Ruby while 語句

語法

while conditional [do]
   code
end

條件為真時執行程式碼while迴圈的條件程式碼之間用保留字 do、換行符或分號 ; 分隔。

示例

#!/usr/bin/ruby

$i = 0
$num = 5

while $i < $num  do
   puts("Inside the loop 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

Ruby while 修飾符

語法

code while condition

OR

begin 
  code 
end while conditional

條件為真時執行程式碼

如果while修飾符跟在一個沒有rescueensure子句的begin語句之後,則在評估條件之前會先執行一次程式碼

示例

#!/usr/bin/ruby

$i = 0
$num = 5
begin
   puts("Inside the loop i = #$i" )
   $i +=1
end while $i < $num

這將產生以下結果:

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

條件為假時執行程式碼until語句的條件與程式碼之間用保留字do、換行符或分號分隔。

示例

#!/usr/bin/ruby

$i = 0
$num = 5

until $i > $num  do
   puts("Inside the loop 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
Inside the loop i = 5

Ruby until 修飾符

語法

code until conditional

OR

begin
   code
end until conditional

條件為假時執行程式碼

如果until修飾符跟在一個沒有rescueensure子句的begin語句之後,則在評估條件之前會先執行一次程式碼

示例

#!/usr/bin/ruby

$i = 0
$num = 5
begin
   puts("Inside the loop i = #$i" )
   $i +=1;
end until $i > $num

這將產生以下結果:

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

表示式中的每個元素執行一次程式碼

示例

#!/usr/bin/ruby

for i in 0..5
   puts "Value of local variable is #{i}"
end

這裡,我們定義了範圍 0..5。語句 for i in 0..5 將允許i取 0 到 5(包括 5)範圍內的值。這將產生以下結果:

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迴圈幾乎完全等效於以下內容:

(expression).each do |variable[, variable...]| code end

不同之處在於for迴圈不會為區域性變數建立新的作用域。for迴圈的表示式程式碼之間用保留字 do、換行符或分號分隔。

示例

#!/usr/bin/ruby

(0..5).each do |i|
   puts "Value of local variable is #{i}"
end

這將產生以下結果:

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

終止最內層的迴圈。如果在程式碼塊內呼叫(方法返回 nil),則終止具有關聯程式碼塊的方法。

示例

#!/usr/bin/ruby

for i in 0..5
   if i > 2 then
      break
   end
   puts "Value of local variable is #{i}"
end

這將產生以下結果:

Value of local variable is 0
Value of local variable is 1
Value of local variable is 2

Ruby next 語句

語法

next

跳轉到最內層迴圈的下一個迭代。如果在程式碼塊內呼叫(yield或呼叫返回 nil),則終止程式碼塊的執行。

示例

#!/usr/bin/ruby

for i in 0..5
   if i < 2 then
      next
   end
   puts "Value of local variable is #{i}"
end

這將產生以下結果:

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

重新啟動最內層迴圈的本次迭代,無需檢查迴圈條件。如果在程式碼塊內呼叫,則重新啟動yieldcall

示例

#!/usr/bin/ruby

for i in 0..5
   if i < 2 then
      puts "Value of local variable is #{i}"
      redo
   end
end

這將產生以下結果,並將進入無限迴圈:

Value of local variable is 0
Value of local variable is 0
............................

Ruby retry 語句

語法

retry

如果retry出現在begin表示式的rescue子句中,則從begin語句體的開頭重新啟動。

begin
   do_something # exception raised
rescue
   # handles error
   retry  # restart from beginning
end

如果retry出現在迭代器、程式碼塊或for表示式的語句體中,則重新啟動迭代器呼叫的執行。迭代器的引數將被重新評估。

for i in 1..5
   retry if some_condition # restart from i == 1
end

示例

#!/usr/bin/ruby
for i in 0..5
   retry if i > 2
puts "Value of local variable is #{i}"
end

這將產生以下結果,並將進入無限迴圈:

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
............................
廣告
© . All rights reserved.