What does ||= mean? [duplicate]
Solution 1:
Basically, a ||= b
means assign b to a if a is null or undefined or false (i.e. false-ish value in ruby)
, it is similar to a = b unless a
, except it will always evaluate to the final value of a
(whereas a = b unless a
would result in nil
if a
was true-ish).
Solution 2:
||= is a ruby idiom. It means if @current_user is nil (or false) then try to find it by id and assign it to @current_user, otherwise do nothing.
See these related questions.
Solution 3:
This is part of Ruby.
If @current_user
is nil or false, it will be set to User.find_by_id(session[:user_id])