Created
September 12, 2012 19:54
-
-
Save jmeirow/3709453 to your computer and use it in GitHub Desktop.
Ruby-style enums
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 DataType | |
def self.STRING | |
:STRING | |
end | |
def self.DATE | |
:DATE | |
end | |
def self.UNKNOWN | |
:UNKNOWN | |
end | |
def self.NUMERIC | |
:NUMERIC | |
end | |
end |
Forked!
Please don't take me like I'm trying to one-up or trying to be a jerk here... I just LOVE to go over code with people! :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By using a class with class methods that return but do not accept a symbol, I can get very close to what is offer by a constant, or an enum in a static language such as C# or Java. If I just used symbols, I’d run the risk of making a typo which may or may not surface as a runtime error. If I mis-type these names, a syntax error will ensue, which is close enough to a compile-time error for me.