开发者

(a && b) versus (a and b)

开发者 https://www.devze.com 2023-03-22 12:28 出处:网络
Given : a=true b=false why can I开发者_JS百科 do : puts [a && b, a || b]#[false, true] but not

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)]
0

精彩评论

暂无评论...
验证码 换一张
取 消