LoginSignup
10
10

More than 5 years have passed since last update.

rubyのblock文でのbreak,next

Posted at

rubyのblock文中で、条件によって処理を飛ばすのにnextが使える。

#5を飛ばす
10.times{|v|
  if v==5
    next
  end
  puts v
}
#出力
#0
#1
#2
#3
#4
#6
#7
#8
#9

ちなみに、breakだと、ブロック処理全体を終了する。

10.times{|v|
  if v==5
    break
  end
  puts v
}

#出力結果
# 0
# 1
# 2
# 3
# 4

10
10
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
10
10