Created
July 21, 2015 06:11
-
-
Save itang/2256658f3b0c23d380c9 to your computer and use it in GitHub Desktop.
crystal macro: reflect
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 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