Why do disjunction and join in words require parentheses?

Disjunction and conjunction of the words ( or

, and

) in the argument positions require additional brackets, unlike ||

, &&

.

def foo _; end

foo(1 || 2) # => Good
foo(1 or 2) # => Syntax error
foo((1 or 2)) # => Good

foo(1 && 2) # => Good
foo(1 and 2) # => Syntax error
foo((1 and 2)) # => Good

      

Why do they need extra parentheses?

+3


source to share


1 answer


I think because logical linking operators and

and or

have lower precedence than method argument list, so parser if no other list argument or closing parenthesis is found.

On the other hand, logical operators &&

and ||

have higher precedence, so their arguments are evaluated earlier and the result of the expression is then passed to the method as an argument.



The offline parenthesis change association, therefore, foo (1 or 2)

works and 1

is passed as a result to the method foo

.

+3


source







All Articles