16
14

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 5 years have passed since last update.

Rubyでの||=(ダブルパイプイコール ・Double Pipe / Or Equals)について

Posted at

Rubyで見かける「||=」という演算子について。

見慣れないのでどういうものかといったものですが、

a ||= b

の意味としては

a = a || b

とイコールの意味のようです。

★What Ruby’s ||= (Double Pipe / Or Equals) Really Does
 http://www.rubyinside.com/what-rubys-double-pipe-or-equals-really-does-5488.html

「パーフェクト Ruby on Rails」の記述で以下があったので調べてました。

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

確かに下のように書くよりスマートですね。

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

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
16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?