Keyword for exclusive or in ruby?

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

    0 讨论(0)
  • 2021-01-04 00:43

    I ran into an issue because the '^' operator acts bitwise on numbers,

    true ^ 1
    => false
    
    1 ^ true
    TypeError: can't convert true into Integer
    true ^ 1
    

    so my workaround was:

    ( !!a ^ !!b ) where the double-bang coerces them into booleans.

    !!1 ^ !!true
    => false
    
    !!1 ^ !!false
    => true
    
    0 讨论(0)
  • 2021-01-04 00:55

    Any implementation of xor won't allow short circuiting. Both expressions need to be evaluated no matter what.

    Ruby does provide the ^ operator, but this will choke on truthy values. I've implemented a function to handle the cases where I want an xor that behaves more like and and or:

    def xor(a,b)
      (a and (not b)) or ((not a) and b)
    end
    

    Unlike ^, this function can be used in situations similar to the following:

    xor("hello".match(/llo/), false) # => true
    xor(nil, 1239)                   # => true
    xor("cupcake", false)            # => false
    
    0 讨论(0)
  • 2021-01-04 00:56

    Try ^

    true  ^ false #=> true
    true  ^ true  #=> false
    false ^ false #=> false
    

    No plain english equivalent operator though.

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2021-01-04 01:06

    No it doesn't, you can only use ^.

    Don't know why there isn't particularly, may just be because it isn't as commonly used.

    0 讨论(0)
提交回复
热议问题