Using "not" in compound conditions

P

Peter Lynch

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
 
E

Eero Saynatkari

--IssJQH/X6nZGf72c
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hi
I am still a beginner at Ruby, and have just arrived at this in my=20
learning curve.
=20
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)
=20
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= =20
like -
(not(condition1)) && (not(condition2))

Alternatives:

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

And my favourite:

not (condition1 or condition2)

--IssJQH/X6nZGf72c
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (FreeBSD)

iD8DBQFFRbAb7Nh7RM4TrhIRAlO0AJ45ugYXpNxyn897FhmYIrzE6r/R9QCg5bW9
cl11Zx4kL/TI76Ti+ONa4xc=
=lgXN
-----END PGP SIGNATURE-----

--IssJQH/X6nZGf72c--
 
J

Justin Collins

Peter said:
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

"&&", "||", 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
 
D

David Vallner

--------------enig21017591C8ADCEA853DB1D75
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Peter said:
Hi
I am still a beginner at Ruby, and have just arrived at this in my=20
learning curve.
=20
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)
=20
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= =20
like -
(not(condition1)) && (not(condition2))
=20
Regards
Peter
=20
=20
=20

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


--------------enig21017591C8ADCEA853DB1D75
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFRn/Oy6MhrS8astoRAliXAJ9gcNFuOSR55bGmYyZ3nlA74/ZBngCfZYWd
kkk9U7qGW/59xdAM2PBn+JE=
=JMd0
-----END PGP SIGNATURE-----

--------------enig21017591C8ADCEA853DB1D75--
 
D

Dale Martenson

Peter said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,217
Messages
2,571,121
Members
47,724
Latest member
Farreach2565

Latest Threads

Top