0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

each などのarrayメソッド時の、break, return, nextの挙動

Posted at

break

ループを抜ける

users.each | user | do
 puts user
 break if user == 'hoge太郎'
end
 →userが「hoge太郎」の時、ループを終了しeach処理を抜ける

return

メソッドを抜ける

class hogehoge
 def puts_name(users)
  users.each | user | do
  puts user
  return if user == 'hoge太郎'
 end
end
 →userが「hoge太郎」の時、hogehogeメソッドを抜ける

next

ループの次の要素に行く

users.each | user | do
 puts user
 next if user == 'hoge太郎'
 puts 'hoge太郎のときは出力されないよ'
end
 →userが「hoge太郎」の時、次の要素に即座に行く、後続の処理(ここで言う、puts)は実行されない
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?