Ruby: Is &! documented anywhere?
A while back, due to a typo, I realized you could use &! in ruby statements:
if this? &! that?
was the same as if this? && !that?
I've scoured all of the ruby operator docs I can find with no mention of it anywhere.Interestingly, this works:
if this? & ! that?
but this does not: if this? & & that?
If I ack the ruby source, I don't see &!, but I do see a ton of &&!Wat? Now I'm curious. Anyone know the innards of ruby enough to enlighten me?
Comments
It doesn't really work quite as you suggest; it's just that
where !b is true if b is nil or false, and false otherwise.& works as a bitwise AND for numbers, and a logical AND for boolean values. You can't mix the two, but as long as a is a boolean, you'll get some kind of result out of the calculation:
Only when working with explicit Booleans, yes. Otherwise no:
In this case, the behavior is inappropriate in that Ruby evaluates only false and nil for False, and everything else as True.I'm going to assume this is some quirk or "feature" as part of the bitwise operator '&'.