Using "not" in compound conditions

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))

Regards
Peter

Peter L. 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.

On 2006.10.30 16:43, Peter L. wrote:

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))

Alternatives:

(not condition1) and (not condition2)
!condition && !condition

And my favourite:

not (condition1 or condition2)

Peter L. wrote:

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))

Regards
Peter

“&&”, “||”, and “!” have higher precedence than “and”, “or” and “not” -
so it can be tricky.

However, you could do:

(not condition1) && (not condition2)

or, not mixing the two:

not condition1 and not condition2

Of course, it’s best to be clear, and parentheses help that. Hope that
helps.

-Justin

Thankyou Dale for pointing me in the right direction.
For anybody else looking at precedence, here is a list of the operator
precedences - The Ruby Language

Peter L. wrote:

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))

Regards
Peter

For compound conditions, I’d use ! instead of not, likewise && and ||
instead of and and or. With copious parentheses. I prefer the word
variants when the result would read like a sentence - with compound
booleans all hope is lost anyway, and I prefer to be able to tell terms
apart from operators easier (one being letters, the other special
characters).

Also, due to the low binding of not, I prefer to use “unless” and
“until” instead of “if not” and “while not”.

Also, I think the (!(condition1) && !(condition2)) is slightly bad form.
Inverting the logic makes things simpler to read: (condition1 ||
condition2). Yay freshman discrete maths. (Although I admit to cheating,
I fired up IDEA to tell me this.)

David