Skip to content

Instantly share code, notes, and snippets.

@judofyr
Created April 25, 2010 19:17
Show Gist options
  • Save judofyr/378650 to your computer and use it in GitHub Desktop.
Save judofyr/378650 to your computer and use it in GitHub Desktop.
## 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 $!))
@judofyr
Copy link
Author

judofyr commented Apr 27, 2010

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