Thursday, May 24, 2007

^ Means XOR In Ruby

Saw something I didn't recognize in the Rails source:

unless types.any? ^ block

Turns out it's an exclusive or operator.

{giles-computer:giles} [05-24 21:59] ~
! irb
>> true ^ false
=> true
>> true ^ true
=> false
>> false ^ false
=> false
>> exit
{giles-computer:giles} [05-24 22:00] ~
!

4 comments:

  1. and c, c++, perl, verilog...

    :)

    http://en.wikipedia.org/wiki/Bitwise_operation#XOR

    ReplyDelete
  2. Well, it's a binary XOR on booleans, a bitwise XOR on numbers and a set XOR on sets.

    And if you want it to mean something else, you can go ahead and define it on your own classes.

    Yay ruby!

    ReplyDelete
  3. Caveat: you can go ahead and define it, but as far as I can tell, you still have to define it to take an arg.

    ReplyDelete

Note: Only a member of this blog may post a comment.