When do we use the "||=" operator in Rails ? What is its significance?

57,665

Solution 1

Lets break it down:

@_current_user ||= {SOMETHING}

This is saying, set @_current_user to {SOMETHING} if it is nil, false, or undefined. Otherwise set it to @_current_user, or in other words, do nothing. An expanded form:

@_current_user || @_current_user = {SOMETHING}

Ok, now onto the right side.

session[:current_user_id] &&
      User.find(session[:current_user_id])

You usually see && with boolean only values, however in Ruby you don't have to do that. The trick here is that if session[:current_user_id] is not nil, and User.find(session[:current_user_id]) is not nil, the expression will evaluate to User.find(session[:current_user_id]) otherwise nil.

So putting it all together in pseudo code:

if defined? @_current_user && @_current_user
  @_current_user = @_current_user
else
  if session[:current_user_id] && User.find(session[:current_user_id])
    @_current_user = User.find(session[:current_user_id])
  else
    @_current_user = nil
  end
end

Solution 2

This is caching abilities.

a ||= 1  # a assign to 1
a ||= 50 # a is already assigned, a will not be assigned again

puts a
#=> 1

this is useful when u load current user from DB, if this is loaded prior, statement will not try to evaluate right part of equation, which DRY, therefore u can consider it as caching operator.

REF: http://railscasts.com/episodes/1-caching-with-instance-variables

Solution 3

First made popular in C, the binary operator shorthand, for example:

a += b      # and...
a ||= b

acts like:

a = a + b   # and ... note the short circuit difference ... 
a || a = b

The rearrangement for a more effective short circuit is a graceful way of dealing with a check for nil as it avoids the assignment altogether if it can. The assignment may have side effects. Just another example of seriously thoughtful design in Ruby.

See http://www.rubyinside.com/what-rubys-double-pipe-or-equals-really-does-5488.html for a more wordy explanation.

Solution 4

If you have C# experience, I believe it is similar ( but more of a Ruby-trick ) to the null-coalescing (??) operator in C#

int? y = x ?? -1

x is assigned to y if x is not null, otherwise the "default" value of -1 is used.

Similarly, ||= is called the T-square operator I believe.

a || = b

or

a || a = b

Coming to your statement

@_current_user ||= session[:current_user_id] &&
    User.find(session[:current_user_id])

Basically, it sees if @_current_user is nil or not. If it has some value, leave it alone ( the current user. ) Else, get the current user from the session using the user id. It first sees if the id is in the session, and then gets from the User.

Look at the blog below for more info on the "T-square" operator:

http://blogs.oracle.com/prashant/entry/the_ruby_t_square_operator

Share:
57,665
geeky_monster
Author by

geeky_monster

Updated on July 08, 2022

Comments

  • geeky_monster
    geeky_monster almost 2 years

    Possible Duplicate:
    What does the operator ||= stands for in ruby?

    I am confused with the usage of ||= operator in Rails. I couldn't locate anything useful on the web. Can anyone please guide me?

    Do let me know if there are any weblinks that you are aware of.

    I would like what the following statement means:

    @_current_user ||= session[:current_user_id] &&
          User.find(session[:current_user_id])