-
-
Save raggi/485442 to your computer and use it in GitHub Desktop.
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
# Example of the implementation | |
class Template | |
module CompileSite; end | |
def compile(klass, name, source) | |
klass.class_eval <<-RUBY | |
lambda {}.binding.__send__ :eval, <<-RB | |
def \#{name}(locals) | |
\#{source} | |
end | |
RB | |
RUBY | |
end | |
end | |
module Helper | |
A = "A" | |
end | |
class Scope | |
include Template::CompileSite | |
include Helper | |
B = "B" | |
end | |
Template.new.compile(Scope, 'hello', <<-RUBY) | |
if locals.nil? | |
yield | |
else | |
[locals, A, B, yield, hello(nil) { 'Y2' }, yield].join(" ") | |
end | |
RUBY | |
res = Scope.new.hello("L") { "Y1" } | |
if res == "L A B Y1 Y2 Y1" | |
puts "YOU DID IT!" | |
else | |
puts "Failed:", res | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment