[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

c/c++ enum equivalent?

benjohn

3/17/2006 4:43:00 PM


I'd like to fill a module with valueless constants (because I don't care
about the actual values, just that they are uneque, and named) - is that
a good thing to do, and can I do that? :)

I tried:

module AnEnum
:ValueOne
:ValueTwo
...
end

But mentioning a constant's sysmbol doesn't define it.




4 Answers

Gregory Seidman

3/17/2006 4:50:00 PM

0

On Sat, Mar 18, 2006 at 01:42:51AM +0900, benjohn@fysh.org wrote:
} I'd like to fill a module with valueless constants (because I don't care
} about the actual values, just that they are uneque, and named) - is that
} a good thing to do, and can I do that? :)
}
} I tried:
}
} module AnEnum
} :ValueOne
} :ValueTwo
} ...
} end
}
} But mentioning a constant's sysmbol doesn't define it.

Those are symbols, not constants. And they are what you want. You don't
need to define them anywhere. They are defined by being used.

--Greg



Robert Klemme

3/17/2006 4:55:00 PM

0

benjohn@fysh.org wrote:
> I'd like to fill a module with valueless constants (because I don't care
> about the actual values, just that they are uneque, and named) - is that
> a good thing to do, and can I do that? :)
>
> I tried:
>
> module AnEnum
> :ValueOne
> :ValueTwo
> ...
> end
>
> But mentioning a constant's sysmbol doesn't define it.

That's true. :-) If you just need the names you can just use symbols
(you don't even have to define them). You could do

module AnEnum
VALUES = [
:foo,
:bar,
:dummy,
].freeze
end

If you look for more sophisticated solutions: There was a posting about
this around 9. and 10. March this year with subject "Enums (was: My Ruby
talk @ work..)". You'll also likely find something in the RAA:

http://raa.ruby...

Kind regards

robert

benjohn

3/20/2006 9:45:00 PM

0


On 17 Mar 2006, at 16:49, Gregory Seidman wrote:

> On Sat, Mar 18, 2006 at 01:42:51AM +0900, benjohn@fysh.org wrote:
> } I'd like to fill a module with valueless constants (because I
> don't care
> } about the actual values, just that they are uneque, and named) -
> is that
> } a good thing to do, and can I do that? :)
> }
> } I tried:
> }
> } module AnEnum
> } :ValueOne
> } :ValueTwo
> } ...
> } end
> }
> } But mentioning a constant's sysmbol doesn't define it.
>
> Those are symbols, not constants. And they are what you want. You
> don't
> need to define them anywhere. They are defined by being used.

Hi Greg, thank you ... I suppose what I wanted was to be able to write:
AnEnum::ValueIDidntDefine

... in some code, and have Ruby come back and bash me for talking
about something that's not in the set of allowed values.


benjohn

3/20/2006 9:47:00 PM

0


On 17 Mar 2006, at 16:58, Robert Klemme wrote:

> benjohn@fysh.org wrote:
>> I'd like to fill a module with valueless constants (because I
>> don't care
>> about the actual values, just that they are uneque, and named) -
>> is that
>> a good thing to do, and can I do that? :)
>> I tried:
>> module AnEnum
>> :ValueOne
>> :ValueTwo
>> ...
>> end
>> But mentioning a constant's sysmbol doesn't define it.
>
> That's true. :-) If you just need the names you can just use
> symbols (you don't even have to define them). You could do
>
> module AnEnum
> VALUES = [
> :foo,
> :bar,
> :dummy,
> ].freeze
> end
>
> If you look for more sophisticated solutions: There was a posting
> about this around 9. and 10. March this year with subject "Enums
> (was: My Ruby talk @ work..)".

Thank you, I will take a look for that thread.

*snips*