Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

operators

ruby

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]) 
like image 537
geeky_monster Avatar asked Apr 17 '11 04:04

geeky_monster


People also ask

What does ||= mean in Ruby?

a ||= b is a conditional assignment operator. It means: if a is undefined or falsey, then evaluate b and set a to the result. Otherwise (if a is defined and evaluates to truthy), then b is not evaluated, and no assignment takes place.

What does :: operator do in Rails?

The :: is a unary operator that allows: constants, instance methods and class methods defined within a class or module, to be accessed from anywhere outside the class or module. Remember in Ruby, classes and methods may be considered constants too.

What is << in Ruby on Rails?

It lets you add items to a collection or even concatenate strings.

What is the name of this operator === in Ruby?

Triple Equals Operator (More Than Equality) Our last operator today is going to be about the triple equals operator ( === ). This one is also a method, and it appears even in places where you wouldn't expect it to. Ruby is calling the === method here on the class.


2 Answers

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 
like image 62
Mike Lewis Avatar answered Sep 28 '22 08:09

Mike Lewis


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

like image 41
c2h2 Avatar answered Sep 28 '22 07:09

c2h2