Ruby-loops

提供:Dev Guides
移動先:案内検索

Ruby-ループ

Rubyのループは、同じコードブロックを指定された回数実行するために使用されます。 この章では、Rubyがサポートするすべてのループステートメントについて詳しく説明します。

Ruby whileステートメント

構文

while conditional [do]
   code
end

_conditional_がtrueのときに_code_を実行します。 _while_ループの_conditional_は、予約語do、改行、バックスラッシュ\、またはセミコロン;によって_code_から分離されています。

#!/usr/bin/ruby

$i = 0
$num = 5

while &dollar;i < &dollar;num  do
   puts("Inside the loop i = #&dollar;i" )
   &dollar;i &plus;=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

_conditional_がtrueのときに_code_を実行します。

_rescue_またはensure句のない_begin_ステートメントの後に_while_修飾子が続く場合、_code_は条件が評価される前に1回実行されます。

#!/usr/bin/ruby

&dollar;i = 0
&dollar;num = 5
begin
   puts("Inside the loop i = #&dollar;i" )
   &dollar;i &plus;=1
end while &dollar;i < &dollar;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 Statement

until conditional [do]
   code
end

conditional_がfalseのときに_code_を実行します。 _until_ステートメントの条件は、予約語_do、改行、またはセミコロンによって_code_から分離されます。

#!/usr/bin/ruby

&dollar;i = 0
&dollar;num = 5

until &dollar;i > &dollar;num  do
   puts("Inside the loop i = #&dollar;i" )
   &dollar;i &plus;=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

修飾子までルビー

構文

code until conditional

OR

begin
   code
end until conditional

_conditional_がfalseのときに_code_を実行します。

_until_修飾子が_rescue_またはensure句のない_begin_ステートメントの後に続く場合、_condition_が評価される前に_code_が1回実行されます。

#!/usr/bin/ruby

&dollar;i = 0
&dollar;num = 5
begin
   puts("Inside the loop i = #&dollar;i" )
   &dollar;i &plus;=1;
end until &dollar;i > &dollar;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

ステートメントのルビー

構文

for variable [, variable ...] in expression [do]
   code
end

_expression_の各要素に対して_code_を1回実行します。

#!/usr/bin/ruby

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

ここで、範囲0..5を定義しました。 0..5の_i_のステートメントにより、_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_ループの_expression_は、予約語do、改行、またはセミコロンによって_code_から分離されます。

#!/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

最も内部的なループの次の反復にジャンプします。 (_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

ループ条件をチェックせずに、最も内部的なループのこの反復を再開します。 ブロック内で呼び出された場合、_yield_または_call_を再起動します。

#!/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_がbegin expressionのrescue句に表示される場合、begin bodyの先頭から再開します。

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

イテレータ、ブロック、または_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
............................