Skip to content

Instantly share code, notes, and snippets.

@Sija
Created January 24, 2021 15:26
Show Gist options
  • Save Sija/247c56da87e89b6922bf2114bc6c35e7 to your computer and use it in GitHub Desktop.
Save Sija/247c56da87e89b6922bf2114bc6c35e7 to your computer and use it in GitHub Desktop.
macro string_enum(name, &block)
enum {{name}}
{% for exp in block.body.expressions %}
{% if exp.is_a?(Assign) %}
{{exp.target}}
{% else %}
{{exp}}
{% end %}
{% end %}
def to_s
case self
{% for exp in block.body.expressions %}
{% if exp.is_a?(Assign) %}
in {{exp.target}}
{{exp.value}}
{% end %}
{% end %}
end
end
end
end
string_enum Numbers do
One = "1"
Two = "2"
Three = "3"
def hello
"Hello!"
end
end
puts Numbers::One
puts Numbers::Two
puts Numbers::Three
puts Numbers::One.hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment