Bug: Syntax error when using 'or' keyword


#1

I am getting a parse error when using the ‘or’ keyword. I do not get
this error with ‘||’.

$ ruby -v
ruby 1.8.4 (2005-12-24) [powerpc-darwin8.3.0]

$ ruby-yarv -v
ruby 2.0.0 (Base: Ruby 1.9.0 2006-02-14) [powerpc-darwin8.5.0]
YARVCore 0.4.0 Rev: 482 (2006-03-08) [opts: ]

def example(arg)
end

example(nil || 3) # -> no error
example(nil or 3) # -> parse error, unexpected kOR, expecting ‘)’

– Daniel


#2

Hi –

On Sat, 25 Mar 2006, Daniel H. wrote:

def example(arg)
end

example(nil || 3) # -> no error
example(nil or 3) # -> parse error, unexpected kOR, expecting ‘)’

There was a thread about this recently on ruby-core. Matz summed up
the situation as follows:

OK. Keyword logical operators (and, or, not) are far lower
precedence
than comma, even lower than method calls without argument parentheses
(in parser, they are called as commands), so that they are not
allowed
be a part of argument expression.

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” chapters now available
from Manning Early Access Program! http://www.manning.com/books/black


#3

On Mar 24, 2006, at 7:22 PM, removed_email_address@domain.invalid wrote:

There was a thread about this recently on ruby-core. Matz summed up
the situation as follows:

OK. Keyword logical operators (and, or, not) are far lower
precedence
than comma, even lower than method calls without argument
parentheses
(in parser, they are called as commands), so that they are not
allowed
be a part of argument expression.

Thanks, I should have checked ruby-core archives before posting.

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/7553

– Daniel


#4

Pistos C. wrote:

Pistos

The “Expressions” section in Chapter 22 of the Pickaxe, 2nd. ed.,
contains a table of the Ruby operators in precedence order and, in the
“Boolean Expressions” section, some prose that explains the and, &&, or,
||, not and !, and defined? operators.


#5

unknown wrote:

OK. Keyword logical operators (and, or, not) are far lower
precedence
than comma, even lower than method calls without argument parentheses
(in parser, they are called as commands), so that they are not
allowed
be a part of argument expression.

Is there documentation anywhere which describes when to use the English
words versus the symbols, and why? All I know is that the words have
far lower precedence, but up to this point, I have been using them in
favour of the symbols whenever possible, using parentheses to force
evaluation order.

More and more, though, I feel “discriminated against” by the language,
in that I should always be using the symbols.

Pistos