Created
October 27, 2009 19:28
-
-
Save corbanbrook/219869 to your computer and use it in GitHub Desktop.
Metaclass Eval
This file contains 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
@variables = Object.new | |
def @variables.metaclass | |
class << self; self; end | |
end | |
def @variables.meta_eval &block | |
metaclass.instance_eval &block | |
end | |
def @variables.meta_def name, &block | |
meta_eval { define_method name, &block } | |
end | |
def @variables.add sym | |
metaclass = class << self; self; end | |
# Three methods for doing the same thing, creating a getter method in the metaclass | |
#metaclass.instance_eval { define_method(sym) { instance_variable_get('@' + sym.to_s) } } | |
#metaclass.send(:define_method, sym) { instance_variable_get('@' + sym.to_s) } | |
metaclass.send(:attr_reader, sym) # like this way the best because its the cleanest | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment