Created
April 25, 2010 19:17
-
-
Save judofyr/378650 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
## Ruby Quiz #666 | |
module Special | |
CONSTANT = 1 | |
end | |
module Base | |
end | |
class Specific | |
include Base | |
include Special | |
# Don't touch anything above. | |
# | |
# Goal: Define a method (foo) under the Base module which | |
# references Specific::CONSTANT and accepts a block with yield: | |
# | |
# module Base | |
# def foo | |
# CONSTANT + yield | |
# end | |
# end | |
# | |
# Ignore the fact that Special exists. The constant could be in any included module. | |
# Failed attempts: | |
# The CONSTANT lookup doesn't work at all. | |
module ::Base | |
def foo | |
CONSTANT + yield | |
end | |
end | |
# The CONSTANT lookup doesn't work in 1.9. | |
::Base.class_eval do | |
def foo2 | |
CONSTANT + yield | |
end | |
end | |
# The CONSTANT lookup works, but the yield doesn't. | |
::Base.send(:define_method, :foo3) do | |
CONSTANT + yield | |
end | |
end | |
## Examples: | |
p ((Specific.new.foo { 2 } rescue $!)) | |
p ((Specific.new.foo2 { 2 } rescue $!)) | |
p ((Specific.new.foo3 { 2 } rescue $!)) |
The point is: All Tilt know is that the Ruby code could be CONSTANT + yield
, so it can't really modify that (without parsing it and doing lots of checks, which is out of scope of Tilt).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I owe you all an apology for giving you a challenge without context.
The code is for Tilt. A fast template engine will generate Ruby code. For instance, the Haml template
%div= yield
could generate"<div>#{yield}</div>"
. The fastest way to evaluate such code, is to define it as a method:Then you can simply call
render { foo }
and you'll get the result.However, since you can set the scope/self to anything in Tilt, and Tilt doesn't want to define those methods on Object, you'll have to do this:
Then Tilt will define the method under
Base
(actuallyTilt::CompileSite
) like this:And Tilt#render will now invoke that method.
Now, this works great except it messes up with the constant scope. You can't reference any constant in Specific, only Base. In 1.8 you can fix this by doing (foo2 above):
But in 1.9 class_eval w/block also changes constant scope :(
If you use define_method (foo3 above), the constant scope works, but
yield
doesn't :/This seems to work in 1.9, but is extremely hackish: