Created
July 22, 2010 10:22
-
-
Save judofyr/485811 to your computer and use it in GitHub Desktop.
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
## Ruby Quiz #666 | |
module CompileSite; end | |
def compile(name, source) | |
CompileSite.class_eval <<-RUBY | |
def #{name}(locals) | |
Thread.current[:tilt_vars] = [self, locals] | |
class << self | |
this, locals = Thread.current[:tilt_vars] | |
this.instance_eval do | |
#{source} | |
end | |
end | |
end | |
RUBY | |
end | |
def render(name, scope, locals, &blk) | |
scope.__send__(name, locals, &blk) | |
end | |
module Helper | |
A = "A" | |
end | |
class Scope | |
include CompileSite | |
include Helper | |
B = "B" | |
end | |
class ScopeB < Scope | |
B = 'C' | |
end | |
compile('hello', <<-RUBY) | |
if locals.nil? | |
yield | |
else | |
[locals, A, B, yield, render(:hello, self, nil) { 'Y2' }, yield, locals].join(" ") | |
end | |
RUBY | |
CompileSite.instance_method(:hello) | |
res = render(:hello, Scope.new, "L") { "Y1" } | |
res2 = render(:hello, ScopeB.new, "L") { "Y1" } | |
if res == "L A B Y1 Y2 Y1 L" and res2 == "L A C Y1 Y2 Y1 L" | |
puts "YOU DID IT!" | |
else | |
puts "Failed:", res, res2 | |
end |
Yeah, I had a feeling you'd simplified that from the original. That's fine... :-)
Good work on the bug reports!
JRuby is weird. This works:
class Base
class_eval <<-RUBY
def hello
class << self
yield
end
end
RUBY
end
p Base.new.hello { 123 }
But this does not:
class Base
#class_eval <<-RUBY
def hello
class << self
yield
end
end
#RUBY
end
p Base.new.hello { 123 }
Yay! The bugs in Rubinius are now closed :-)
awesome
Merged. Passes all tests on 1.8.7. I'm pulling down the latest 1.9.1 now. Review appreciated.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I should also mention that it currently defines different methods depending on the locals you pass in. It actually "unrolls" the locals so the source actually looks like this:
So if we define the method directly on the scope, it means that a total of L * S methods will be defined (where L is the number of variations of lvars and S is the number of variations of scope).