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 $!)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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).