I have a protected method in my application contoller
def current_user
@current_user ||= User.find_by_id(session[:user_id])
end
I was wondering what ||= means?
I’ve been trying to search and find out, but to no avail.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Basically,
a ||= bmeansassign b to a if a is null or undefined or false (i.e. false-ish value in ruby), it is similar toa = b unless a, except it will always evaluate to the final value ofa(whereasa = b unless awould result innilifawas true-ish).