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?

More than 3 years have passed since last update.

| | = とは

Last updated at Posted at 2021-03-13

||= を見かけたので調べてみた。

=だけならいつも通りのはずが、前に||がついていたので違いをメモ。

nilガードというらしい。


@current_user = User.find(session[:user_id])

上記だと、@current_userにUser.find(session[:user_id])を代入することになる。


@current_user ||= User.find(session[:user_id])

一方||=だと、@current_userがnilまたはfalseの場合の時のみUser.find(session[:user_id])が代入される。

つまり、||=の場合は正しい値が入っていれば、代入されない。

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?