Skip to content

Instantly share code, notes, and snippets.

@trans
Created June 24, 2009 10:52
Show Gist options
  • Save trans/135156 to your computer and use it in GitHub Desktop.
Save trans/135156 to your computer and use it in GitHub Desktop.
EnumeratedType (define numerical constants quickly)
# 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