[#1816] Ruby 1.5.3 under Tru64 (Alpha)? — Clemens Hintze <clemens.hintze@...>

Hi all,

17 messages 2000/03/14

[#1989] English Ruby/Gtk Tutorial? — schneik@...

18 messages 2000/03/17

[#2241] setter() for local variables — ts <decoux@...>

18 messages 2000/03/29

[ruby-talk:01836] Re: enum examples?

From: Dave Thomas <Dave@...>
Date: 2000-03-15 13:58:00 UTC
List: ruby-talk #1836
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

In This Thread