Skip to content

Instantly share code, notes, and snippets.

@xpepper
Last active December 16, 2015 10:28
Show Gist options
  • Save xpepper/5419976 to your computer and use it in GitHub Desktop.
Save xpepper/5419976 to your computer and use it in GitHub Desktop.
class Enum
def self.new
Class.new do
def initialize(name)
@name = name
end
def to_s
"#{self.class.name}: #{@name}"
end
def self.const_missing(name)
const_set(name, new(name))
end
end
end
end
Color = Enum.new
ThreatLevel = Enum.new
puts Color::Red # => Color: Red
puts Color::Blue # => Color: Blue
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