# Syntax bug, in 1.8.5? return not (some expr) \<-- syntax error vs return (not (some expr)) \<-- fine

**URL:** <https://rubytalk.org/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-error-vs-return-not-some-expr-fine/39309>\
**Category:** ruby-talk\
**Created:** [22 July 2007 07:35 UTC](https://rubytalk.org/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-error-vs-return-not-some-expr-fine/39309 "2007-07-22T07:35:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Good\_Night\_Moon](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@Good\_Night\_Moon](https://rubytalk.org/u/Good_Night_Moon)\
**Post date:** [22 July 2007 07:35 UTC](https://rubytalk.org/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-error-vs-return-not-some-expr-fine/39309/1 "2007-07-22T07:35:05Z")

</div>

Well, it's all in the subject, to me it looks like a parsing bug:

class MyRect  
&nbsp;&nbsp;def intersects\_good(other)  
&nbsp;&nbsp;&nbsp;&nbsp;return (not ((other.x1 \< self.x0 or self.x1 \< other.x0) and  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(other.y1 \< self.y0 or self.y1 \< other.y0)))  
&nbsp;&nbsp;end

&nbsp;&nbsp;def intersects\_bad(other)  
&nbsp;&nbsp;&nbsp;&nbsp;return not ((other.x1 \< self.x0 or self.x1 \< other.x0) and  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(other.y1 \< self.y0 or self.y1 \< other.y0))  
&nbsp;&nbsp;end  
end

&nbsp;&nbsp;\> ruby -c myrect.rb  
&nbsp;&nbsp;myrect.rb:8: syntax error, unexpected kNOT, expecting kEND  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return not ((other.x1 \< self.x0 or self.x1 \< other.x0) and  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^

Any comment? Is this known? Or just some hair in the Ruby grammar?

---

<div class="post-metadata">

**Author:** ![David\_and\_Sharon\_Phi](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@David\_and\_Sharon\_Phi](https://rubytalk.org/u/David_and_Sharon_Phi)\
**Post date:** [22 July 2007 07:51 UTC](https://rubytalk.org/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-error-vs-return-not-some-expr-fine/39309/2 "2007-07-22T07:51:26Z")

</div>

There's a difference between [not, and, or] and [!, &&, ||]  
Not sure what it is, but I've found it better to use the latter.  
Still, if you wrap the whole statement in another set of brackets (like intersects\_good) it will work.

&nbsp;&nbsp;&nbsp;def intersects\_bad(other)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (not ((other.x1 \< self.x0 or self.x1 \< other.x0) and  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(other.y1 \< self.y0 or self.y1 \< other.y0)))  
&nbsp;&nbsp;&nbsp;end

by the way, what's the difference between the two methods? They appear identical.

Cheers,  
Dave

> **···**
>
> On 22/07/2007, at 5:35 PM, Good Night Moon wrote:
> 
> > Well, it's all in the subject, to me it looks like a parsing bug:
> > 
> > class MyRect  
> > &nbsp;&nbsp;def intersects\_good(other)  
> > &nbsp;&nbsp;&nbsp;&nbsp;return (not ((other.x1 \< self.x0 or self.x1 \< other.x0) and  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(other.y1 \< self.y0 or self.y1 \< other.y0)))  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def intersects\_bad(other)  
> > &nbsp;&nbsp;&nbsp;&nbsp;return not ((other.x1 \< self.x0 or self.x1 \< other.x0) and  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(other.y1 \< self.y0 or self.y1 \< other.y0))  
> > &nbsp;&nbsp;end  
> > end
> > 
> > &nbsp;&nbsp;\> ruby -c myrect.rb  
> > &nbsp;&nbsp;myrect.rb:8: syntax error, unexpected kNOT, expecting kEND  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return not ((other.x1 \< self.x0 or self.x1 \< other.x0) and  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^
> > 
> > Any comment? Is this known? Or just some hair in the Ruby grammar?

---

<div class="post-metadata">

**Author:** ![BIl\_Kleb1](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@BIl\_Kleb1](https://rubytalk.org/u/BIl_Kleb1)\
**Post date:** [22 July 2007 07:54 UTC](https://rubytalk.org/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-error-vs-return-not-some-expr-fine/39309/3 "2007-07-22T07:54:59Z")

</div>

Good Night Moon wrote:

> &nbsp;&nbsp;myrect.rb:8: syntax error, unexpected kNOT, expecting kEND  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return not ((other.x1 \< self.x0 or self.x1 \< other.x0) and

You need to use the '!' operator instead of the 'not' "conjunction".  
It's a precedence thing -- the parser is seeing 'return not' -- see  
parse.y in the source.

Regards,

> **···**
>
> --  
> Bil Kleb  
> [http://fun3d.larc.nasa.gov](http://fun3d.larc.nasa.gov)

---

<div class="post-metadata">

**Author:** ![Good\_Night\_Moon](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@Good\_Night\_Moon](https://rubytalk.org/u/Good_Night_Moon)\
**Post date:** [22 July 2007 20:30 UTC](https://rubytalk.org/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-error-vs-return-not-some-expr-fine/39309/4 "2007-07-22T20:30:02Z")

</div>

Thanks, maybe I'll look. It's not just precedence though, or else  
'return' itself is part of it:

&nbsp;&nbsp;def ok  
&nbsp;&nbsp;&nbsp;&nbsp;return (not true)  
&nbsp;&nbsp;end

&nbsp;&nbsp;def fails  
&nbsp;&nbsp;&nbsp;&nbsp;return not true  
&nbsp;&nbsp;end

And note that within an 'if', it works as you'd expect:

&nbsp;&nbsp;def with\_parens\_ok  
&nbsp;&nbsp;&nbsp;&nbsp;if (not true)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "not true"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;def no\_parens\_ok  
&nbsp;&nbsp;&nbsp;&nbsp;if not true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "not true"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

> **···**
>
> On Sun, 22 Jul 2007 03:50:01 -0400, Bil Kleb wrote:
> 
> > > &nbsp;&nbsp;myrect.rb:8: syntax error, unexpected kNOT, expecting kEND  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return not ((other.x1 \< self.x0 or self.x1 \< other.x0) and
> > 
> > You need to use the '!' operator instead of the 'not' "conjunction".  
> > It's a precedence thing -- the parser is seeing 'return not' -- see  
> > parse.y in the source.

---

<div class="post-metadata">

**Author:** ![Good\_Night\_Moon](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@Good\_Night\_Moon](https://rubytalk.org/u/Good_Night_Moon)\
**Post date:** [22 July 2007 20:34 UTC](https://rubytalk.org/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-error-vs-return-not-some-expr-fine/39309/5 "2007-07-22T20:34:59Z")

</div>

Thanks. The difference is just to point out what looks like  
a bug; the parens shouldn't make a difference. Look at  
my other reply in this thread for elaboration.

> **···**
>
> On Sun, 22 Jul 2007 16:51:26 +0900, Sharon Phillips wrote:
> 
> > There's a difference between [not, and, or] and [!, &&, ||] Not sure  
> > what it is, but I've found it better to use the latter. Still, if you  
> > wrap the whole statement in another set of brackets (like  
> > intersects\_good) it will work.
> > 
> > &nbsp;&nbsp;&nbsp;def intersects\_bad(other)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (not ((other.x1 \< self.x0 or self.x1 \< other.x0) and  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(other.y1 \< self.y0 or self.y1 \< other.y0)))  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > by the way, what's the difference between the two methods? They appear  
> > identical.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [22 July 2007 21:11 UTC](https://rubytalk.org/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-error-vs-return-not-some-expr-fine/39309/6 "2007-07-22T21:11:11Z")

</div>

Hi --

> > There's a difference between [not, and, or] and [!, &&, ||] Not sure  
> > what it is, but I've found it better to use the latter. Still, if you  
> > wrap the whole statement in another set of brackets (like  
> > intersects\_good) it will work.
> > 
> > &nbsp;&nbsp;&nbsp;def intersects\_bad(other)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (not ((other.x1 \< self.x0 or self.x1 \< other.x0) and  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(other.y1 \< self.y0 or self.y1 \< other.y0)))  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > by the way, what's the difference between the two methods? They appear  
> > identical.
> 
> Thanks. The difference is just to point out what looks like  
> a bug; the parens shouldn't make a difference. Look at  
> my other reply in this thread for elaboration.

I don't think it's a bug. Have a look at these examples:

def x; return 1 and puts "I'm gone!"; end  
SyntaxError: compile error  
(irb):3: void value expression

def x; 2 not 3; end  
SyntaxError: compile error  
(irb):5: syntax error, unexpected kNOT, expecting kEND

These both make sense. 'and' is right-associative, so you're trying  
to do something after having already returned, which doesn't work.  
And in general you can't do "x not y", which is what "return not ..."  
is read as.

David

> **···**
>
> On Mon, 23 Jul 2007, Good Night Moon wrote:
> 
> > On Sun, 22 Jul 2007 16:51:26 +0900, Sharon Phillips wrote:
> 
> --  
> \* Books:  
> &nbsp;&nbsp;&nbsp;RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242\](http://www.awprofessional.com/title/0321509242%5C))  
> &nbsp;&nbsp;&nbsp;RUBY FOR RAILS ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> \* Ruby/Rails training  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Greg\_Brown1](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@Greg\_Brown1](https://rubytalk.org/u/Greg_Brown1)\
**Post date:** [23 July 2007 01:16 UTC](https://rubytalk.org/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-error-vs-return-not-some-expr-fine/39309/7 "2007-07-23T01:16:18Z")

</div>

if binds differently than return. It is precedence.

> **···**
>
> On 7/22/07, Good Night Moon \<no.email.here@zombo.com\> wrote:
> 
> > On Sun, 22 Jul 2007 03:50:01 -0400, Bil Kleb wrote:  
> > \>\> myrect.rb:8: syntax error, unexpected kNOT, expecting kEND  
> > \>\> return not ((other.x1 \< self.x0 or self.x1 \< other.x0) and  
> > \>  
> > \> You need to use the '!' operator instead of the 'not' "conjunction".  
> > \> It's a precedence thing -- the parser is seeing 'return not' -- see  
> > \> parse.y in the source.
> > 
> > Thanks, maybe I'll look. It's not just precedence though, or else  
> > 'return' itself is part of it:

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [23 July 2007 22:22 UTC](https://rubytalk.org/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-error-vs-return-not-some-expr-fine/39309/8 "2007-07-23T22:22:27Z")

</div>

I agree that  
if not File.exists? "a"  
&nbsp;&nbsp;do stuff  
end

would be nice intuitively. I wish...

unknown wrote:

> **···**
>
> > Hi --
> > 
> > On Mon, 23 Jul 2007, Good Night Moon wrote:
> > 
> > > > by the way, what's the difference between the two methods? They appear  
> > > > identical.
> > > 
> > > Thanks. The difference is just to point out what looks like  
> > > a bug; the parens shouldn't make a difference. Look at  
> > > my other reply in this thread for elaboration.
> > 
> > I don't think it's a bug. Have a look at these examples:
> > 
> > def x; return 1 and puts "I'm gone!"; end  
> > SyntaxError: compile error  
> > (irb):3: void value expression
> > 
> > def x; 2 not 3; end  
> > SyntaxError: compile error  
> > (irb):5: syntax error, unexpected kNOT, expecting kEND
> > 
> > These both make sense. 'and' is right-associative, so you're trying  
> > to do something after having already returned, which doesn't work.  
> > And in general you can't do "x not y", which is what "return not ..."  
> > is read as.
> > 
> > David
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [25 July 2007 16:51 UTC](https://rubytalk.org/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-error-vs-return-not-some-expr-fine/39309/9 "2007-07-25T16:51:34Z")

</div>

Except that return isn't an operator. As far as I can tell, looking  
at parse.y for ruby1.8.6 the parse tree for the original statement  
which raised the question

return not true

should be:

command\_call  
&nbsp;&nbsp;kRETURN expr  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;kNOT expr  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;primary  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var\_ref  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;variable  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;kTRUE

There definitely looks to be something strange here.

> **···**
>
> On 7/22/07, Gregory Brown \<gregory.t.brown@gmail.com\> wrote:
> 
> > On 7/22/07, Good Night Moon \<no.email.here@zombo.com\> wrote:  
> > \> On Sun, 22 Jul 2007 03:50:01 -0400, Bil Kleb wrote:  
> > \> \>\> myrect.rb:8: syntax error, unexpected kNOT, expecting kEND  
> > \> \>\> return not ((other.x1 \< self.x0 or self.x1 \< other.x0) and  
> > \> \>  
> > \> \> You need to use the '!' operator instead of the 'not' "conjunction".  
> > \> \> It's a precedence thing -- the parser is seeing 'return not' -- see  
> > \> \> parse.y in the source.  
> > \>  
> > \> Thanks, maybe I'll look. It's not just precedence though, or else  
> > \> 'return' itself is part of it:
> > 
> > if binds differently than return. It is precedence.
> 
> --  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![F\_Senault](https://avatars.discourse-cdn.com/v4/letter/f/c37758/32.png) [@F\_Senault](https://rubytalk.org/u/F_Senault)\
**Post date:** [23 July 2007 22:35 UTC](https://rubytalk.org/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-error-vs-return-not-some-expr-fine/39309/10 "2007-07-23T22:35:03Z")

</div>

unless File.exists? "a"  
&nbsp;&nbsp;do stuff  
end

do stuff unless File.exists? "a"

Isn't it even more intuitive ?

Fred

> **···**
>
> Le 24 juillet 2007 à 00:22, Roger Pack a écrit :
> 
> > I agree that  
> > if not File.exists? "a"  
> > &nbsp;&nbsp;do stuff  
> > end
> > 
> > would be nice intuitively. I wish...
> 
> --  
> If you no longer admin for the fsckers do they really exist?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Vagn Scott in the SDM)
