Keyword for exclusive or in ruby?

后端 未结 7 1934
广开言路
广开言路 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 01:07

    John's answer appears incorrect. In irb with 1.9.3, xor("cupcake", false) returns true, as you'd expect.

    1.9.3-p429 :104 > def xor(a,b)
    1.9.3-p429 :105?>     (a and (not b)) or ((not a) and b)
    1.9.3-p429 :106?>   end
     => nil 
    1.9.3-p429 :107 > xor(false, true)
     => true 
    1.9.3-p429 :108 > xor("cupcake", false)
     => true 
    
    0 讨论(0)
提交回复
热议问题