Skip to content

Instantly share code, notes, and snippets.

@shuber
Created September 28, 2011 04:36
Show Gist options
  • Save shuber/1246999 to your computer and use it in GitHub Desktop.
Save shuber/1246999 to your computer and use it in GitHub Desktop.
LoadEval proof of concept
module LoadEval
class << self
def contexts
@contexts ||= []
end
def load_within_context(object, file)
contexts << [object, file]
load('load_eval_proxy.rb', true)
ensure
contexts.pop
end
end
def load_eval(file)
LoadEval.load_within_context(self, file.sub(/^([^\.]+)$/, '\1.rb'))
end
end
instance_variables = method(:instance_variables)
class << self
instance_methods.each { |method| undef_method method }
def method_missing(method, *arguments, &block)
LoadEval.contexts.last.first.__send__(method, *arguments, &block)
end
end
load LoadEval.contexts.last.last
instance_variables.call.each { |variable| instance_variable_set(variable, eval(variable.to_s)) }
class LoadEvalTest
def call_me
puts 'I was called'
end
end
LoadEvalTest.new.load_eval('load_me_in_context')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment