Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ||= mean? [duplicate]

Tags:

ruby

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.

like image 738
pka Avatar asked Sep 26 '11 14:09

pka


People also ask

What is that term that means a duplicate?

Some common synonyms of duplicate are copy, facsimile, replica, and reproduction. While all these words mean "a thing made to closely resemble another," duplicate implies a double or counterpart exactly corresponding to another thing.

What are 9 copies called?

Septuplicate Definition & Meaning - Merriam-Webster.

Does duplicate mean double?

The verb duplicate is pronounced differently, with a long a sound, and it means to make a copy of or to multiply times two. The Latin root, duplicatus, means "to double." “Could you please duplicate this letter for me?”

What's the difference between replicate and duplicate?

Duplicate may be used as a noun, verb or adjective. Related words are duplicates, duplicated, duplicating, duplication. The word duplicate is derived from the Latin word duplicare, which means to double. Replicate means to reproduce something, to construct a copy of something, to make a facsimile.


1 Answers

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).

like image 102
Romain Avatar answered Oct 15 '22 00:10

Romain