LoginSignup
0
0

More than 5 years have passed since last update.

Arrayを使ったnilガード

Last updated at Posted at 2014-05-16

例えば、params[:items]に配列が入っていた時にnilだとエラーになる。

params[:items].each do |item_id|
  #処理
end

この時エラーになってしまうので

Array(params[:items]).each do |item_id|
  #処理
end

とすると、いい感じ。

Array([1,2,3])
# => [1,2,3]

Array(nil)
# => []
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