Given :
a=true
b=false
why can I开发者_JS百科 do :
puts [a && b, a || b] #[false, true]
but not
puts [a and b, a or b]
syntax error, unexpected keyword_and, expecting ']' puts [a and b, a or b]
Apparently, the operator precedence for the comma is higher than "and" but lower than &&.
Putting parenthesis around the elements works:
[(a and b), (a or b)]
you need to simply group the terms to avoid precedence issues:
puts [(a and b),(a or b)]
精彩评论