Skip to content

Instantly share code, notes, and snippets.

@raggi
Forked from judofyr/Example.rb
Created July 22, 2010 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raggi/485442 to your computer and use it in GitHub Desktop.
Save raggi/485442 to your computer and use it in GitHub Desktop.
# 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