Skip to content

Instantly share code, notes, and snippets.

@corbanbrook
Created October 27, 2009 19:28
Show Gist options
  • Save corbanbrook/219869 to your computer and use it in GitHub Desktop.
Save corbanbrook/219869 to your computer and use it in GitHub Desktop.
Metaclass Eval
@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