[#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:01865] Re: enum examples?

From: Clemens Hintze <c.hintze@...>
Date: 2000-03-15 21:13:54 UTC
List: ruby-talk #1865
Hi,

Hugh Sasse Staff Elec Eng writes:
> On Wed, 15 Mar 2000, Yukihiro Matsumoto wrote:
> 
> > Hi,
> > 
> > In message "[ruby-talk:01834] enum examples?"
> >     on 00/03/15, Hugh Sasse Staff Elec Eng <hgs@dmu.ac.uk> writes:

...

> I was at cross purposes then.  I was thinking of enumerated types
> as in Pascal and C++.

matz has already show you an implementation that could be used to
solve your special problem. But I cannot resist to show you an
example, that realize a Enumeration like type as in Pascal or
C++. :-)))

   def Enumeration(*args)
     args << :dummy    
     mod = Module.new
     name, value = "", -1
     for arg in args
       raise SyntaxError, "Enumerator have to begin with [A-Z]: (#{name})" \
	 unless name.capitalize == name
       mod.module_eval "#{name} = #{value}" unless name.empty?
       if arg.type == String
	 name, value = arg, value+1
       elsif arg.type == Symbol
	 name, value = arg.id2name, value+1
       elsif arg.type <= Numeric
	 value = arg
       else
	 raise TypeError
       end
     end
     mod
   end

You could use this method like:

   spam = Enumeration("Gnark",2, "Blubb",7, "Foo", :Bar, :Foobar)

Then you may access such a constant via e.g.:
  
   spam::Blubb
   spam::Bar
   ...

This definition would be written in C++ as:

   enum spam { Gnark=2, Blubb=7, Foo, Bar, Foobar };

...

> > 
> > Re-inventing wheel is fun using Ruby. :-)
> > 

Yeah! That it is, really! :-))))

...

> 	Hugh
> 	hgs@dmu.ac.uk

\cle

-- 
Clemens Hintze  mailto: c.hintze@gmx.net

In This Thread