Skip to content

Instantly share code, notes, and snippets.

@rklemme
Created May 20, 2011 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rklemme/983088 to your computer and use it in GitHub Desktop.
Save rklemme/983088 to your computer and use it in GitHub Desktop.
Yet another enum...
class Enum
module EnumMember
attr_reader :args
def [](idx)
args[idx]
end
end
class <<self
include Enumerable
def each(&b)
values.each(&b)
self
end
def values
constants.map {|c| const_get(c)}
end
def enum(*args)
cl = Class.new(self)
cl.instance_variable_set("@args", args.freeze)
cl.extend(EnumMember).freeze
end
def inherited(cl)
class <<cl
private :new, :allocate
end
end
end
end
class GenericState < Enum
ACCEPTED = enum "foo"
UNSURE = enum
end
p GenericState.values
GenericState.each {|s| p s}
p GenericState.map(&:name)
p GenericState::ACCEPTED[0]
GenericState.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment