# C/c++ enum equivalent?

**URL:** <https://www.ruby-forum.com/t/c-c-enum-equivalent/83238>\
**Category:** Ruby\
**Created:** [March 17, 2006, 5:43pm UTC](https://www.ruby-forum.com/t/c-c-enum-equivalent/83238 "2006-03-17T17:43:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 17, 2006, 5:43pm UTC](https://www.ruby-forum.com/t/c-c-enum-equivalent/83238/1 "2006-03-17T17:43:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 17, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/c-c-enum-equivalent/83238/2 "2006-03-17T17:52:49Z")

</div>

On Sat, Mar 18, 2006 at 01:42:51AM +0900, removed\_email\_address@domain.invalid 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 17, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/c-c-enum-equivalent/83238/3 "2006-03-17T18:02:00Z")

</div>

removed\_email\_address@domain.invalid wrote:

> 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-lang.org/](http://raa.ruby-lang.org/)

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 19, 2006, 1:35am UTC](https://www.ruby-forum.com/t/c-c-enum-equivalent/83238/4 "2006-03-19T01:35:31Z")

</div>

On 3/17/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’d like to fill a module with valueless constants (because I don’t care  
> about the actual values, just that they are unique, and named)

# -------±--------±--------±------- +

--------±--------±--------±--------+

# Add methods enum and bit\_enum to the Object class, thus making it

much

# easier and less error-prone to define a long list of constants in a

# class. This idea comes from Ara.T.Howard@noaa, in a posting to

ruby-talk

# on August 2, 2005 (in reply to a question). These work very nicely

with

# a word-list array as generated via %w(). Very clever!

# 

class Object  
def enum\_constants(\*list)  
mc = Module === self ? self : self.class  
list.flatten.each\_with\_index{|e, i| mc.const\_set e.to\_s.intern, i}  
end  
def enum\_bit\_constants(\*list)  
mc = Module === self ? self : self.class  
list.flatten.each\_with\_index{|e, i| mc.const\_set e.to\_s.intern, 2 \*\*  
i}  
end  
end

# -------±--------±--------±------- +

--------±--------±--------±--------+

An example of use:

```
# Constants that define all categories of packets which we 

```

recognize.  
enum\_constants %w(  
SBC\_NONE SBC\_ARRAY SBC\_GOODPW SBC\_GOODKEY SBC\_GOODNONE  
SBC\_BADPW SBC\_BADKEY SBC\_BADNONE SBC\_IGNFAIL SBC\_ALL  
SBC\_UNMATCHED  
) # “UNMATCHED” should be the last one.  
SBC\_ENUMCOUNT = SBC\_UNMATCHED + 1;

I’ve been meaning to add a few more bells and whistles to this,  
such as having a way for ‘enum\_constants’ to automatically  
handle the definition of values such as SBC\_ENUMCOUNT.  
But of course I have a few million things that “I’ve been meaning  
to do”, and never get around to.

Someone else had a module of some sort for enums. I forget  
who, but it was someone on this mailing list in the last month  
or so. Their version was very different from the above, and it  
in some situations what they did would have some significant  
advantages over the simple solution I like to use.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 20, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/c-c-enum-equivalent/83238/5 "2006-03-20T22:47:18Z")

</div>

On 17 Mar 2006, at 16:58, Robert K. wrote:

> > …  
> > :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_

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 20, 2006, 10:45pm UTC](https://www.ruby-forum.com/t/c-c-enum-equivalent/83238/6 "2006-03-20T22:45:51Z")

</div>

On 17 Mar 2006, at 16:49, Gregory S. wrote:

> } :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.
