On Mon, Aug 11, 2003 at 02:02:33AM +0900, dblack / superlink.net wrote:
> > Does ruby not have exclusive or? I can't remember at the moment. If it
> > did, I think the top is the same
> > as:
> >
> > unless a.nil? ^ b.nil?
> >
> > Which is still somewhat unclear, I agree.
> 
> I, however, no long agree with myself :-)  I just forgot about ^.

However ^ is overloaded, so although it works when comparing false/true with
false/true, it doesn't with other things; especially since it doubles up as
a bitwise numeric operator too.

irb(main):001:0> 5 ^ false
TypeError: cannot convert false into Integer
        from (irb):1:in `^'
        from (irb):1

But it does treat 'nil' as 'false'. So you could say:

     if  (a && true) ^ (b && true)
       ...
     end

which tests if both a and b are 'true' or both a and b are 'false' in the
Ruby sense of those terms, but it's still not very pretty. I think it would
be clearer to write

     if  (a && b) || (!a && !b)

It's not a problem with nil? anyway. If you want to check whether a and b
are either both zero or both nonzero, you could do

     if (a == 0) == (b == 0)
       ...
     end

The fact that nonzero? carries forward the original value, rather than just
'true', is IMO a nice touch which allows useful chaining of operations like
cmp. But that's just being pragmatic :-)

Cheers,

Brian.