On Tue, Sep 28, 2010 at 4:29 PM, Jim Haungs <jhaungs / gmail.com> wrote:
> Interesting. Thanks.
>
> One sees control code like this in rails all the time:
>
> redirect_to <somewhere> and return if <condition>
>
> Never having looked at the return value from redirect_to (but guessing
> it's always non-nil), I guess I assumed 'and' was the non-short-circuit
> operator.
>
> Otherwise, why this is idiom so prevalent? =A0Because it looks more
> English-like?

Because it was the recommended way to get around 'double render'
errors in the early days of Rails, and yes and is more English-like.

The problem is that the and and or operators have a surprisingly low
precedence, so for example
a =3D 1
b =3D 2
x =3D a >0 and b < 1

sets x to true, then evaluates b < 1 returning false and throws the result =
away.

I've seen too many problems caused by this that I eschew and in favor
of && almost? everywhere in my ruby code.


--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale