Contents
Ruby – ループ~RUBY入門〜RUBYTUTORIAL〜
Rubyのループは、指定された回数だけ同じコードブロックを実行するために使用されます。この章では、Rubyでサポートされているすべてのループ文について詳しく説明します。
Ruby whileステートメント
構文
while conditional [do] code end
conditionalがtrueのときにコードを実行します。
例
#!/usr/bin/ruby $i = 0 $num = 5 while $i < $num do puts(" iは = #$i" ) $i +=1 end
これにより、次の結果が生成されます。
iは = 0 iは = 1 iは = 2 iは = 3 iは = 4
Ruby while修飾子
構文
code while condition OR begin code end while conditional
conditionalがtrueのときにコードを実行します。
while修飾子では、while
の後の条件式
がtrueを返す間、前に書かれた式
を繰り返し実行する。
例
#!/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
conditionalがfalseのときにコードを実行します。つまり下記ですとnum=5にあたるまでループをし続けます
例
#!/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 for文
構文
code until conditional OR begin code end until conditional
conditionalがfalseのときにコードを実行します。
until修飾子がレスキューまたは句を指定しないbegin文の後に続く場合、conditionalが評価される前にコードが1回実行されます。
例
#!/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
expressionの各要素に対してコードを 1回実行します。
例
#!/usr/bin/ruby for i in 0..5 puts "Value of local variable is #{i}" end
ここでは、範囲0..5を定義しました。もちろん0~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
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
while
文の中で redo
と書くと、そこでループの先頭に戻る。戻ったときに while
の条件式は評価されず、無条件にループが再開されるようになります。
例
#!/usr/bin/ruby for i in 0..5 if i < 2 then puts "答えは #{i}" redo end end
これにより、次の結果が得られ、無限ループに入ります。
答えは 0 答えは 0 ............................
Ruby retry文
構文
retry
begin式のレスキュー句にretryが表示された場合は、beginボディの先頭から再起動してください。
begin do_something # exception raised rescue # handles error retry # restart from beginning end
イテレータ、ブロック、またはfor式の本体にretryが表示された場合は、イテレータ呼び出しの呼び出しを再開します。イテレータへの引数が再評価されます。
for i in 1..5 retry if some_condition # restart from i == 1 end
例
#!/usr/bin/ruby for i in 0..5 if i > 2 puts "答えは #{i}" end
これにより、次の結果が得られ、無限ループに入ります。
答えは 1 答えは 2 答えは 1 答えは 2 答えは 1 答えは 2 ............................