13

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.

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
pka
  • 379
  • 1
  • 3
  • 12
  • Not sure if the name applies, but in C# is called the null-coalescing operator http://msdn.microsoft.com/en-us/library/ms173224.aspx – kenny Sep 26 '11 at 14:53
  • Thank you everyone for helping me out and putting me in the right direction! – pka Sep 26 '11 at 15:07
  • 3
    Duplicate: [What does `||=` mean in Ruby?](http://StackOverflow.Com/q/995593/), [What does `||=` mean in Ruby?](http://StackOverflow.Com/q/3800957/), [what is `||=` in ruby?](http://StackOverflow.Com/q/3945711/), [Double Pipe Symbols in Ruby Variable Assignment?](http://StackOverflow.Com/q/4500375/), [What does the “`||=`” operand stand for in ruby](http://StackOverflow.Com/q/5124930/), [what does a `||=` mean in Ruby language?](http://StackOverflow.Com/q/5230162/), [Is the ruby operator `||=` intelligent?](http://StackOverflow.Com/q/2989862/) and probably many others as well. – Jörg W Mittag Sep 27 '11 at 11:11
  • See also [The definitive list of `||=` (OR Equal) threads and pages](http://Ruby-Forum.Com/topic/151660/). – Jörg W Mittag Sep 27 '11 at 11:11

6 Answers6

28

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

Romain
  • 12,679
  • 3
  • 41
  • 54
  • 1
    Also check out *Conditional assignment* on [Ruby Programming/Syntax/Operators](http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Operators#1._Assignment). – rdvdijk Sep 26 '11 at 15:12
  • 1
    Important to realise it is short circuiting as well - `b` will not be run if `a` is `false-ish` (saving time by not doing an expensive db query, for example). – Callum Rogers Sep 26 '11 at 17:41
  • 2
    it's not `a = b unless a`. Please see http://www.rubyinside.com/what-rubys-double-pipe-or-equals-really-does-5488.html for better understanding how it actually works. – Sigurd Oct 28 '13 at 14:03
  • 2
    It's not the same as `a = b unless a`. `a ||= b` always returns `a` (after the assignment if applicable), whereas `a = b unless a` returns `nil` if `a` is trueish. This is a very significant distinction, as this mechanism is often used to cache a heavy operation. – Pelle May 23 '14 at 23:18
5

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

Community
  • 1
  • 1
m-sharp
  • 16,443
  • 1
  • 26
  • 26
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])

Chris Ledet
  • 11,458
  • 7
  • 39
  • 47
1

Notice the parallels with a += b, which is equivalent to a = a + b.

So a ||= b is equivalent to a = a || b. As others have mentioned, this is the same as a = b unless a.

grifaton
  • 3,986
  • 4
  • 30
  • 42
  • 1
    Actually, Ruby should not assign in case `a` is not `false-ish`, so that `a = a || b` isn't strictly equivalent to `a = b unless a`. One might think specifically of implications in case `a` happens to be a `Hash` for instance. – Romain Sep 26 '11 at 15:07
  • No, it isn't, as has been discussed several times already here on SO, dozens of times on the Ruby mailinglists and literally hundreds of times in a myriad of blog posts. – Jörg W Mittag Sep 27 '11 at 11:16
  • Oh, right. Can you give me an an example of when this is not the case? – grifaton Sep 27 '11 at 12:24
  • It's not the same as `a = b unless a`. `a ||= b` always returns `a` (after the assignment if applicable), whereas `a = b unless a` returns `nil` if `a` is trueish. This is a very significant distinction, as this mechanism is often used to cache a heavy operation. – Pelle May 23 '14 at 23:17
0

In ruby 'a ||= b' is called "or - equal" operator. It is a short way of saying if a has a boolean value of true(if it is neither false or nil) it has the value of a. If not it has the value of b.

jmoon90
  • 339
  • 4
  • 17
0

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 a shortcut to a = b unless a.

share|edit answered Sep 26 '11 at 14:48

Romain 6,9711330

In addition to this answer here`s an example -

arr = nil || []
arr0 ||= []

arr <=> arr0    *#=> 0*

This means arr expression and arr0 expression are equal.

Hope this helps to understand better ||= operator.

Andrius
  • 717
  • 10
  • 16