Hugh Sasse Staff Elec Eng <hgs / dmu.ac.uk> writes:

> Has anyone any examplse of using the Enumerable module?  I've had a 
> look around, but not found much.  I'm thinking of something to allow
> me to have names for bits in a flag field, so I can test if one or
> more is set.  I could create my own object for this, of course, but
> why re-invent unless I must?

Clemens has give a great answer about Enumerable.

To name your bits, you could use class constants


     class StopSign
       RED   = 1 << 0
       AMBER = 1 << 1
       GREEN = 1 << 2

       attr_accessor :state

       def initialize(aState)
         state = aState
       end
     end


     s = StopSign.new(StopSign::GREEN)

     s.state  = StopSign::RED
     s.state |= StopSign::AMBER


This isn't perfect - rather than using the default write accessor,
you'd probably want to write your own that checks the parameter is
valid. In fact, you'd probably not implement a stop sign this way at
all! However, it shows the idea.

Regards


Dave