From: Dan Doel Date: 2003-08-11T03:24:24+09:00 Subject: Re: Ruby and OOP-design (question of an old "procedural person" ;) Brian Candler wrote: >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 > > Yeah, I thought of the problem with ^ on the way to lunch. And I just checked and there's no "xor" equivalent to "and" "or" and "not", so you can't work around it that way. Perhaps an "xor" keyword (or a ^^ logical xor operator) would be handy to have specifically for this kind of situation, since there's no way to conveniently do (obj1 xor obj2) in existance tests. Then again, I can't think of any places where you'd need to do that. - Dan