# Unless Expression Parsing

**URL:** <https://www.ruby-forum.com/t/unless-expression-parsing/92508>\
**Category:** Ruby\
**Created:** [April 20, 2007, 3:54am UTC](https://www.ruby-forum.com/t/unless-expression-parsing/92508 "2007-04-20T03:54:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pfharlock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@pfharlock](https://www.ruby-forum.com/u/pfharlock)\
**Post date:** [April 20, 2007, 3:54am UTC](https://www.ruby-forum.com/t/unless-expression-parsing/92508/1 "2007-04-20T03:54:25Z")

</div>

I just came across an unexpected parsing problem:

x = 3 unless true # parses just fine, x = nil

p(3 unless true) # parser chokes on unless

p((3 unless true)) # OK, ‘nil’ is printed

This seems like a bug in the parser rather than a real  
syntax problem, but maybe I’m missing something?

Gary W.

---

<div class="post-metadata">

**Author:** ![pfharlock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@pfharlock](https://www.ruby-forum.com/u/pfharlock)\
**Post date:** [April 20, 2007, 5:52am UTC](https://www.ruby-forum.com/t/unless-expression-parsing/92508/2 "2007-04-20T05:52:32Z")

</div>

On Apr 19, 9:53 pm, Gary W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Gary W.

in irb:

x = 3 unless true =\> nil

x =\> nil # x declared as a nil  
object since it was never assigned an object  
p 3 unless true =\> nil

p (( 3 unless true)) =\> nil # you’re asking for the result  
of ‘3 unless true’, )

p nil =\> # no result since p did  
nothing

3 unless true =\> # the result is nil but not  
captured  
so  
p (3 unless true) =\> # no result since you are asking  
p to print nothing

Note:

the above is equivalent to  
unless true so ‘p 3’ never gets  
interpreted and nothing happens  
p 3  
end

---

<div class="post-metadata">

**Author:** ![pfharlock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@pfharlock](https://www.ruby-forum.com/u/pfharlock)\
**Post date:** [April 20, 2007, 7:06am UTC](https://www.ruby-forum.com/t/unless-expression-parsing/92508/3 "2007-04-20T07:06:37Z")

</div>

On Apr 19, 2007, at 11:50 PM, bbiker wrote:

> p (( 3 unless true)) =\> nil # you’re asking for the result  
> of ‘3 unless true’, )

if ‘x unless y’ is a valid expression then why is the _extra_ set  
of parenthesis needed when that expression is used as a method argument?

foo(x unless y) # syntax error  
foo((x unless y)) # not a syntax error

This seems to be a problem with any of the statement modifier forms:  
if, rescue, while, until.

Looking at parse.y it seems that the grammar differentiates between  
arguments and statements. Adding the parens forces the statement to  
become an expression.
