From: "Jesús Gabriel y Galán" Date: 2012-02-10T21:03:41+09:00 Subject: Re: How does && work in ruby On Fri, Feb 10, 2012 at 12:38 PM, Phil Carter wrote: > Hi there, > > Can someone explain to me how the && operator works (in simple terms - > I'm a Ruby newbie! (That even rhymes! ;-) > > More specifically, why does this piece of code assign a user object (or > nil) to @current_user, not true or false? > > @current_user = current_user_session && current_user_session.user It's the "and" operator. It evaluates from left to right its operands, stopping when one of them is falsy (nil or false). The fact that it stops when it finds the first value that will render the whole expression false is called "short circuit". If all operands are truthy (not nil nor false) it returns the last operand. The above line is (roughly) equivalent to: @current_user = current_user_session.nil? ? nil : current_user_session.user I said roughly because it's different if current_user_session is false, but due to the usage I would guess that the intent was to check for nil only. Jesus.