Skip to content

Instantly share code, notes, and snippets.

@itang
Created July 21, 2015 06:11
Show Gist options
  • Select an option

  • Save itang/2256658f3b0c23d380c9 to your computer and use it in GitHub Desktop.

Select an option

Save itang/2256658f3b0c23d380c9 to your computer and use it in GitHub Desktop.
crystal macro: reflect
class A
def name
"itang"
end
end
macro field(obj, name)
if {{obj}}.responds_to? {{name}}
{{obj}}.{{name.id}}
end
end
a = A.new
p field(a, :name)
p field(a, :id)
##output
#"itang"
#nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment