From: Dave Aronson Date: 2013-01-06T09:12:36+09:00 Subject: Re: Best practice for &&, ||, and, or On Sat, Jan 5, 2013 at 5:46 PM, Jan E. wrote: > And how is the low precendence of and/or "dangerous"? Isn't that > exactly what you want in an "if" statement? To have the logical > operators executed at the very end? No! No! 1000.times :no! ;-) Consider the difference between "x = true && false" and "x = true and false". Since && has HIGHER precedence than =, "x = true && false" is equivalent to "x = (true && false)", just as you might expect. The whole expression (including the assignment) evaluates to false, and x is assigned false. No problem. But since "and" has LOWER precedence than =, "x = true and false" is equivalent to "(x = true) and false". You smell something wrong already, don't you? The whole expression (including assignment) evaluates to false again... but lo and behold, x is true! That's because you've left the boolean operators wait until the end, as you believe might be so harmless. The analogous situation for || versus "or" is left as an exercise for the reader. BTW, these two were already part of a talk I've been working on, on Ruby Gotchas, coming soon to a RUG near you. Well, at least near me. ;-) -Dave -- Dave Aronson, the T. Rex of Codosaurus LLC, secret-cleared freelance software developer taking contracts in or near NoVa or remote. See information at http://www.Codosaur.us/.