Created
September 28, 2011 04:36
-
-
Save shuber/1246999 to your computer and use it in GitHub Desktop.
LoadEval proof of concept
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
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 |
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
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)) } |
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
class LoadEvalTest | |
def call_me | |
puts 'I was called' | |
end | |
end | |
LoadEvalTest.new.load_eval('load_me_in_context') |
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
call_me |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment