Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > More concise code

Reply
Thread Tools

More concise code

 
 
barjunk
Guest
Posts: n/a
 
      04-26-2007
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.

 
Reply With Quote
 
 
 
 
Alex Gutteridge
Guest
Posts: n/a
 
      04-26-2007
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 Gutteridge

Bioinformatics Center
Kyoto University



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help making a method more concise Mark Hayes Ruby 7 05-03-2011 07:57 AM
A More Concise Description of Numpy than the Guide to Numpy? W. eWatson Python 2 11-23-2009 08:58 PM
Re: More concise syntax for addressing std::vector* ??? Balog Pal C++ 15 03-23-2009 12:39 PM
concise code (beginner) Bruno Desthuilliers Python 27 09-10-2007 09:29 AM
SQL: writing more concise paramaterized SQL darrel ASP .Net 13 03-30-2006 03:59 PM



Advertisments