Skip to content

Instantly share code, notes, and snippets.

@xpepper
Created April 19, 2013 12:15
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 xpepper/5419968 to your computer and use it in GitHub Desktop.
Save xpepper/5419968 to your computer and use it in GitHub Desktop.
class Enum
def self.new
Class.new do
def self.const_missing(name)
const_set(name, new)
end
end
end
end
Color = Enum.new
ThreatLevel = Enum.new
puts Color::Red # => #<Color:0x100189b28>
puts Color::Blue # => #<Color:0x100189a60>
puts Color::Red != Color::Blue # => true
puts Color::Red == Color::Red # => true
puts Color::Orange != ThreatLevel::Orange # => true
p Color.constants # => ["Blue", "Red", "Orange"]
p ThreatLevel.constants # => ["Orange"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment