Created
June 24, 2009 10:52
-
-
Save trans/135156 to your computer and use it in GitHub Desktop.
EnumeratedType (define numerical constants quickly)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# EnumeratedType lets you create enumerated types like this: | |
# | |
# class OutputType < EnumeratedType | |
# AUTODETECT | |
# UNKNOWN | |
# NOSOUND | |
# WAVWRITER | |
# DSOUND | |
# WINMM | |
# ASIO | |
# OSS | |
# ALSA | |
# ESD | |
# SOUNDMANAGER | |
# COREAUDIO | |
# XBOX | |
# PS2 | |
# GC | |
# XBOX360 | |
# PSP | |
# end | |
# | |
# If you want to start with a different integer than 0, you can just do: | |
# | |
# class OutputType < EnumeratedType | |
# start 15 | |
# AUTODETECT | |
# ... | |
# end | |
# | |
# You can also use start anywhere in the list, to have subsequent constants | |
# enumerated starting with the given value. | |
# | |
# (c)2005 Jamis Buck | |
class EnumeratedType | |
class << self | |
def start(n, orable=false) | |
@next_value = n | |
@orable = orable | |
end | |
def const_missing(sym) | |
@next_value ||= 0 | |
const_set(sym, @next_value) | |
if @orable | |
@next_value *= 2 | |
else | |
@next_value += 1 | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment