Forum: Ruby-core Parsing of 'not'

Posted by Brian Candler (candlerb)
on 2010-09-01 11:19
(Received via mailing list)
Is the following intentional?

>> puts(not 3 < 4)
SyntaxError: compile error
(irb):4: syntax error, unexpected kNOT, expecting ')'
puts(not 3 < 4)
        ^
(irb):4: syntax error, unexpected ')', expecting $end
  from (irb):4
  from :0

However, it works with double parentheses:

>> puts((not 3 < 4))
false

Tested with: ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]

I stumbled across this when trying to do:

  assert not a < b

Regards,

Brian.

P.S. In any case it seems to be difficult to use the value of 'not', 
unless
you add parentheses.

>> not 3 < 4
=> false
>> a = not 3 < 4
SyntaxError: compile error
(irb):2: syntax error, unexpected kNOT
a = not 3 < 4
       ^
  from (irb):2
  from :0
>> a = (not 3 < 4)
=> false

I realise that 'and' and 'or' have precedence lower than assignment, but
'not' is a unary operator.  Perl has no problem with the following:

perl -e 'print not 4 < 3'            # prints 1
perl -e '$a = not 4 < 3; print $a'   # prints 1
Posted by Yusuke ENDOH (Guest)
on 2010-09-03 05:35
(Received via mailing list)
Hi,

2010/9/1 Brian Candler <B.Candler@pobox.com>:
> Is the following intentional?
>
>>> puts(not 3 < 4)
> SyntaxError: compile error
> (irb):4: syntax error, unexpected kNOT, expecting ')'
> puts(not 3 < 4)
>        ^
> (irb):4: syntax error, unexpected ')', expecting $end
>      from (irb):4
>      from :0


My personal opinion is that it is NOT intentional "willingly".  But
it would be difficult to fix it because of yacc's restriction.

If anyone successfully write a patch that causes no parser conflict
nor compatibility problem, the patch will be probably accepted, I
guess.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.