On Thu, Feb 09, 2006 at 08:35:51AM +0900, Matthew Moss wrote:
> > But I have to explicitly use it. Ruby should use duck typing for boolean
> > expression like it does it in many other cases.
> 
> The problem is, if you do that one place (i.e. "oh, this is
> bitwise-and, each bit represents a bool"), then you have to do it
> everywhere, since bitwise-and is NOT limited to single-bit flag checks
> (e.g. it works great for masking as well). Doing it everywhere is a
> very bad thing.
> 
> Which means you either need to propose an extension to Ruby for a new
> "bitwise-and-as-boolean" operator (i.e. flags ?& 0x01 or similar), or
> you need to abstract out a bit more and turn (flags & 0x01) into
> something valid. With a little wrapper func, it can look quite nice:
> 

Until here I agree with you.

> class Something
>    def enabled?
>      @flags[0x01].nonzero?
>    end
> end
> 

I think this is a bad example at it does not what you think it does.
While flags & 0x01 != 0 compares the least significant bit flags[0x01]
does not. You're of by one. It will also not work for multibit checks like
flags & 0x1C.

> And that's assuming someone hasn't already made a bitfield type class
> that makes all of this even prettier.
> 

We will see.

-- 
:wq Claudio