David Vallner
10/30/2006 10:42:00 PM
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))
>
> 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