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

**URL:** <https://www.ruby-forum.com/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-err/110062>\
**Category:** Ruby\
**Created:** [July 22, 2007, 9:36am UTC](https://www.ruby-forum.com/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-err/110062 "2007-07-22T09:36:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Good\_Night\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d9b06d/32.png) [@Good\_Night\_M](https://www.ruby-forum.com/u/Good_Night_M)\
**Post date:** [July 22, 2007, 9:36am UTC](https://www.ruby-forum.com/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-err/110062/1 "2007-07-22T09:36:32Z")

</div>

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

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

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

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

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

---

<div class="post-metadata">

**Author:** ![Good\_Night\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d9b06d/32.png) [@Good\_Night\_M](https://www.ruby-forum.com/u/Good_Night_M)\
**Post date:** [July 22, 2007, 9:51am UTC](https://www.ruby-forum.com/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-err/110062/2 "2007-07-22T09:51:49Z")

</div>

On 22/07/2007, at 5:35 PM, Good Night M. wrote:

> ```
> (other.y1 < self.y0 or self.y1 < other.y0))
> 
> ```
> 
> end  
> end
> 
> foo\> ruby -c myrect.rb  
> myrect.rb:8: syntax error, unexpected kNOT, expecting kEND  
> return not ((other.x1 \< self.x0 or self.x1 \< other.x0) and  
> ^
> 
> Any comment? Is this known? Or just some hair in the Ruby grammar?

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.

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

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

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![Good\_Night\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d9b06d/32.png) [@Good\_Night\_M](https://www.ruby-forum.com/u/Good_Night_M)\
**Post date:** [July 22, 2007, 10:35pm UTC](https://www.ruby-forum.com/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-err/110062/3 "2007-07-22T22:35:35Z")

</div>

On Sun, 22 Jul 2007 16:51:26 +0900, Sharon P. 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.

---

<div class="post-metadata">

**Author:** ![Good\_Night\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d9b06d/32.png) [@Good\_Night\_M](https://www.ruby-forum.com/u/Good_Night_M)\
**Post date:** [July 22, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-err/110062/4 "2007-07-22T23:12:39Z")

</div>

Hi –

On Mon, 23 Jul 2007, Good Night M. 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

---

<div class="post-metadata">

**Author:** ![Good\_Night\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d9b06d/32.png) [@Good\_Night\_M](https://www.ruby-forum.com/u/Good_Night_M)\
**Post date:** [July 24, 2007, 12:22am UTC](https://www.ruby-forum.com/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-err/110062/5 "2007-07-24T00:22:26Z")

</div>

I agree that  
if not File.exists? “a”  
do stuff  
end

would be nice intuitively. I wish…

unknown wrote:

> Hi –
> 
> On Mon, 23 Jul 2007, Good Night M. 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

---

<div class="post-metadata">

**Author:** ![Good\_Night\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d9b06d/32.png) [@Good\_Night\_M](https://www.ruby-forum.com/u/Good_Night_M)\
**Post date:** [July 24, 2007, 12:37am UTC](https://www.ruby-forum.com/t/syntax-bug-in-1-8-5-return-not-some-expr-syntax-err/110062/6 "2007-07-24T00:37:44Z")

</div>

Le 24 juillet 2007 à 00:22, Roger P. a écrit :

> I agree that  
> if not File.exists? “a”  
> do stuff  
> end
> 
> would be nice intuitively. I wish…

unless File.exists? “a”  
do stuff  
end

do stuff unless File.exists? “a”

Isn’t it even more intuitive ?

Fred
