c/c++ enum equivalent?

B

benjohn

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.
 
G

Gregory Seidman

On Sat, Mar 18, 2006 at 01:42:51AM +0900, (e-mail address removed) 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
 
R

Robert Klemme

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-lang.org/

Kind regards

robert
 
B

Benjohn Barnes

On Sat, Mar 18, 2006 at 01:42:51AM +0900, (e-mail address removed) 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.
 
B

Benjohn Barnes

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*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top