From: Dale Martenson Date: 2006-11-02T05:58:01+09:00 Subject: Re: Using "not" in compound conditions Peter Lynch wrote: > Hi > I am still a beginner at Ruby, and have just arrived at this in my > learning curve. > > I would like to code > not(condition1) && not(condition2) > This is accepted in irb - > not(condition1) && condition2 > but this produces a syntax error > condition1 && not(condition2) > > I do not get it (obviously). I do not even know what to ask in reply - > How do I code a compound condition, without introducing extra wrappers > like - > (not(condition1)) && (not(condition2)) > I never use the 'not' logical negation. I always use '!'. There is a difference between 'not' and '!'; and that is precedence. '!' is higher than '&&' '&&' is higher than 'not' 'not' is higher than 'and' If you mix logical operators so that precedence is not properly maintained, you end up with weird results. In your 'condition1 && not(condition2)' case, the '&&' will be acted on first rather than the 'not'. This results on the right side of the '&&' have yet to be processed which results in the syntax error. As you learned, adding parathesis changes precedence and corrects the problem. The real solution is to not mix logical operators. condition1 && !condition2 or condition1 and not(condition2) both work. -- Posted via http://www.ruby-forum.com/.