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

From: Hugh Sasse Staff Elec Eng <hgs@...>
Date: 2000-03-16 10:09:11 UTC
List: ruby-talk #1877
On Wed, 15 Mar 2000, Clemens Hintze wrote:

> 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

This looks interesting.  I will have to study it closely.
	
> 
> 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 };
> 

So I think I would need another type to allow variables to ony accept
values and bitwise OR's of values from such a type, and nothing outside
that range.  I'll give this more thought.

> ...
> -- 
> Clemens Hintze  mailto: c.hintze@gmx.net
> 
	Thank you,
	Hugh
	hgs@dmu.ac.uk


In This Thread

Prev Next