Keyword for exclusive or in ruby?

后端 未结 7 1992
广开言路
广开言路 2021-01-04 00:03

Does Ruby have a plain-English keyword for exclusive or, like they have \"and\" and \"or\"? If not, is this because exclusive or doesn\'t allow evaluation short-cutting?

7条回答
  •  醉话见心
    2021-01-04 00:57

    Firstly, I don't think shortcircuiting can sensibly apply to XOR: whatever the value of the first operand, the second needs to be examined.

    Secondly, and, &&, or and || use shortcircuiting in all cases; the only difference between the "word" and "symbol" versions is precedence. I believe that and and or are present to provide the same function as perl has in lines like

    process_without_error or die
    

    I think the reason for not having a xor named function is probably that there's no point in a low-precedence operator in this case and that it's already a confusing enough situation!

提交回复
热议问题