Skip to content

Instantly share code, notes, and snippets.

@jmeirow
Created September 12, 2012 19:54
Show Gist options
  • Save jmeirow/3709453 to your computer and use it in GitHub Desktop.
Save jmeirow/3709453 to your computer and use it in GitHub Desktop.
Ruby-style enums
class DataType
def self.STRING
:STRING
end
def self.DATE
:DATE
end
def self.UNKNOWN
:UNKNOWN
end
def self.NUMERIC
:NUMERIC
end
end
@jmeirow
Copy link
Author

jmeirow commented Sep 12, 2012

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.

@darrencauthon
Copy link

Forked!

@darrencauthon
Copy link

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