LoginSignup
13
10

More than 5 years have passed since last update.

[Ruby on Rails] ループの最初と最後のみ特別扱いする書き方

Posted at

rubyでループで処理するときに、最初だけor最後だけに特別な処理をしたい場合の書き方メモ

@hoges.each_with_index do |hoge, i|

  if i == 0
   最初だけにしたい処理
  end

  共通の処理

  if i == @hoge.length - 1
    最後だけにしたい処理
  end

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