# More concise code

**URL:** <https://www.ruby-forum.com/t/more-concise-code/92973>\
**Category:** Ruby\
**Created:** [April 26, 2007, 6:10am UTC](https://www.ruby-forum.com/t/more-concise-code/92973 "2007-04-26T06:10:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![barjunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@barjunk](https://www.ruby-forum.com/u/barjunk)\
**Post date:** [April 26, 2007, 6:10am UTC](https://www.ruby-forum.com/t/more-concise-code/92973/1 "2007-04-26T06:10:57Z")

</div>

I found this snippet in code called acts\_as\_authenticated. I thought  
it interesting but hard to understand at first.

@current\_user ||= (session[:user] && User.find\_by\_id(session[:user]))  
|| :false

Now for some possible embarrassment.

@current\_user should return a value unless it is nil.

If nil(false),  
then @current\_user is set to session[:user] if session[:user] &&  
User.find\_by\_id return true

otherwise, @current\_user becomes false

Am I even close?

Mike B.

---

<div class="post-metadata">

**Author:** ![barjunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@barjunk](https://www.ruby-forum.com/u/barjunk)\
**Post date:** [April 26, 2007, 7:24am UTC](https://www.ruby-forum.com/t/more-concise-code/92973/2 "2007-04-26T07:24:01Z")

</div>

On 26 Apr 2007, at 13:10, barjunk wrote:

> I found this snippet in code called acts\_as\_authenticated. I thought  
> it interesting but hard to understand at first.
> 
> @current\_user ||= (session[:user] && User.find\_by\_id(session[:user]))  
> || :false
> 
> Now for some possible embarrassment.
> 
> @current\_user should return a value unless it is nil.

Perhaps I’m misunderstanding what you’re saying here, but I think  
it’s the opposite meaning. @current\_user will be set to a new value  
(returned by the right hand side of the expression) unless it is  
already true. I.e. if it is false or nil it will be set to the RHS.  
Remember x ||= y expands to x = x || y

> If nil(false),  
> then @current\_user is set to session[:user] if session[:user] &&  
> User.find\_by\_id return true
> 
> otherwise, @current\_user becomes false
> 
> Am I even close?
> 
> Mike B.

Almost. I’m not sure what the first line of your pseudo code means,  
but the second line is a little bit wrong. @current\_user is set to  
the result of User.find\_by\_id(session[:user]), not session[:user].

Here’s my expanded Ruby code interpretation:

unless @current\_user #Unless we already have a user set  
if session[:user] and User.find\_by\_id(session[:user]) #Test if we  
have session user and id  
@current\_user = User.find\_by\_id(session[:user]) #If we do then  
set user to id  
else  
@current\_user = :false #other wise user is set to :false  
end  
end

Some irb might help with all the ||=, ||, && line noise:

irb(main):004:0\> foo = nil  
=\> nil  
irb(main):005:0\> bar = true  
=\> true  
irb(main):006:0\> foo ||= bar  
=\> true  
irb(main):007:0\> “foo” || “bar”  
=\> “foo”  
irb(main):008:0\> “foo” && “bar”  
=\> “bar”

Alex G.

Bioinformatics Center  
Kyoto University
