# Using "not" in compound conditions

**URL:** <https://www.ruby-forum.com/t/using-not-in-compound-conditions/74102>\
**Category:** Ruby\
**Created:** [October 30, 2006, 8:43am UTC](https://www.ruby-forum.com/t/using-not-in-compound-conditions/74102 "2006-10-30T08:43:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [October 30, 2006, 8:43am UTC](https://www.ruby-forum.com/t/using-not-in-compound-conditions/74102/1 "2006-10-30T08:43:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [November 1, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/using-not-in-compound-conditions/74102/2 "2006-11-01T21:57:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [November 7, 2006, 10:44am UTC](https://www.ruby-forum.com/t/using-not-in-compound-conditions/74102/3 "2006-11-07T10:44:46Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [November 7, 2006, 10:44am UTC](https://www.ruby-forum.com/t/using-not-in-compound-conditions/74102/4 "2006-11-07T10:44:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [November 2, 2006, 3:21pm UTC](https://www.ruby-forum.com/t/using-not-in-compound-conditions/74102/5 "2006-11-02T15:21:04Z")

</div>

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](http://phrogz.net/ProgrammingRuby/language.html#table_18.4)

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [November 7, 2006, 10:47am UTC](https://www.ruby-forum.com/t/using-not-in-compound-conditions/74102/6 "2006-11-07T10:47:03Z")

</div>

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
