Keyword for exclusive or in ruby?

后端 未结 7 1929
广开言路
广开言路 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:42

    As an alternative to Matt Van Horn's double negation trick for using XOR on arbitrary types, you can chain another XOR test, starting with nil. i.e.:

    !!foo ^ !!bar
    

    is equivalent to

    nil ^ foo ^ bar
    

    This looks neater to my eye, and I suppose requires one less logical operation

提交回复
热议问题