1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rubyのループ中,とある条件では,処理を行わないには

Last updated at Posted at 2015-10-09

ループ処理中に,ある条件のときだけは処理を行わず
次のデータを処理したい.

Rubyの前によく使っていたVBAでだっら
(gotoは使ってはいけないと習ったが)
ラベルと go to を使って 

For i = 1 To 10
    if cht = "A" then goto skip
    ...
    skip:
Next

としていたが,Rubyでは

for i in 1..10 # next はここにジャンプ
  next if i==5
  ...
end

としたら,iが5のときのみ処理を飛ばせると知った.

たとえば,コメント行を読み飛ばしたいときに使える.

「Rubyレシピブック」にも書いてあるのだが,かなりの間
気がつかないでいた.

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?