Last active
December 16, 2015 10:28
-
-
Save xpepper/5419976 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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