# Syntax: eval( "a = !a) != eval( "a = not a")

**URL:** <https://rubytalk.org/t/syntax-eval-a-a-eval-a-not-a/10826>\
**Category:** ruby-talk\
**Created:** [31 March 2004 05:56 UTC](https://rubytalk.org/t/syntax-eval-a-a-eval-a-not-a/10826 "2004-03-31T05:56:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mehr\_Assaph\_Assaph](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@Mehr\_Assaph\_Assaph](https://rubytalk.org/u/Mehr_Assaph_Assaph)\
**Post date:** [31 March 2004 05:56 UTC](https://rubytalk.org/t/syntax-eval-a-a-eval-a-not-a/10826/1 "2004-03-31T05:56:22Z")

</div>

> Is there a reason why eval( “a = not a”) raises a syntax error  
> whereas eval( “a = !a”) works as I expected ?

Probably because the statement “a = not a” is also a syntax error  
(regardless of the eval).  
The ‘not’ operator has a lower precedence than assignment, whereas the !  
operator has a higher precedence. Therefore the interpreter reads the  
second form as “a = not” which is a syntax error.  
If you really want to use the word form use: eval(“a = (not a)”), which  
will take care of precedence.

See:  
[http://phrogz.net/ProgrammingRuby/tut\_expressions.html#definedandorandno](http://phrogz.net/ProgrammingRuby/tut_expressions.html#definedandorandno)  
t  
and [http://phrogz.net/ProgrammingRuby/language.html](http://phrogz.net/ProgrammingRuby/language.html) the section on  
Boolean Expressions and table\_18.4.

---

<div class="post-metadata">

**Author:** ![Jean-Hugues\_ROBERT](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@Jean-Hugues\_ROBERT](https://rubytalk.org/u/Jean-Hugues_ROBERT)\
**Post date:** [31 March 2004 11:12 UTC](https://rubytalk.org/t/syntax-eval-a-a-eval-a-not-a/10826/2 "2004-03-31T11:12:22Z")

</div>

Hi,  
OK, so precedence of: not, is lower than precedence of: =. Thanks.  
Yet…

> > Is there a reason why eval( “a = not a”) raises a syntax error  
> > whereas eval( “a = !a”) works as I expected ?
> 
> Probably because the statement “a = not a” is also a syntax error  
> (regardless of the eval).  
> The ‘not’ operator has a lower precedence than assignment, whereas the !  
> operator has a higher precedence. Therefore the interpreter reads the  
> second form as “a = not” which is a syntax error.  
> If you really want to use the word form use: eval(“a = (not a)”), which  
> will take care of precedence.
> 
> See:  
> [Expressions](http://phrogz.net/ProgrammingRuby/tut_expressions.html#definedandorandno)  
> t  
> and [The Ruby Language](http://phrogz.net/ProgrammingRuby/language.html) the section on  
> Boolean Expressions and table\_18.4.  
> I now understand that thing are happening the way they are documented.

For the sake of curiosity, I would still like to know _why_ things  
were designed this way. A way that makes “not” rather useless to me  
(besides the rare case where I would want to avoid a redefined !()).

i.e. why is “not” 's precedence lower than assignment’s one ???

Thanks

Jean-Hugues

> **···**
>
> At 14:56 31/03/2004 +0900, Mehr, Assaph (Assaph) wrote:
> 
> * * *
> 
> Web: [@jhr is virteal, virtually real](http://hdl.handle.net/1030.37/1.1)  
> Phone: +33 (0) 4 92 27 74 17

---

<div class="post-metadata">

**Author:** ![Mark\_Hubbart](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@Mark\_Hubbart](https://rubytalk.org/u/Mark_Hubbart)\
**Post date:** [31 March 2004 16:20 UTC](https://rubytalk.org/t/syntax-eval-a-a-eval-a-not-a/10826/3 "2004-03-31T16:20:48Z")

</div>

The “and”, “or” and “not” operators all have lower precedence. If you  
need higher precedence, use “&&” “||” and “!”. The word-based boolean  
operators are provided as a counterpoint to the higher priority symbol  
ones. The lower precedence allows you to get away with less parenthesis  
in certain situations.

\*\>\> x = 23 == 23 && ! y = 42 == 23 #=\> false  
\*\>\> [x,y] #=\> [false, true]  
\*\>\> x = 23 == 23 and not y = 42 == 23 #=\> false  
\*\>\> [x,y] #=\> [true, true]

–Mark

> **···**
>
> On Mar 31, 2004, at 3:12 AM, Jean-Hugues ROBERT wrote:
> 
> > For the sake of curiosity, I would still like to know _why_ things  
> > were designed this way. A way that makes “not” rather useless to me  
> > (besides the rare case where I would want to avoid a redefined !()).
> > 
> > i.e. why is “not” 's precedence lower than assignment’s one ???
