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 $!))
@mtodd
Copy link

mtodd commented Apr 25, 2010

I got it to work with a little:

class Specific
  include Base
  include Special
  
  module ::Base
    def foo
      self.class.const_get(:CONSTANT) + yield
    end
  end
  
end

@nakajima
Copy link

Yea, that or this:

class Specific
  include Base
  include Special

  def foo4
    self.class::CONSTANT + yield
  end
end

@judofyr
Copy link
Author

judofyr commented Apr 27, 2010

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:

def render
  "<div>#{yield}</div>"
end

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:

class Specific
  include Base # Actually called Tilt::CompileSite
end

Then Tilt will define the method under Base (actually Tilt::CompileSite) like this:

Base.class_eval <<-EOF
  def #{name}
    #{code}
  end
EOF

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):

Specific.class_eval <<-EOF
  # class_eval with string changes constant scope
  Base.class_eval do
    # class_eval with block doesn't
    def #{name}
      #{code}
    end
  end
EOF

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:

Specific.class_eval <<-RUBY
  def self.definer
    Base.send(:define_method, #{name.inspect}) do |locals, &blk|
      Thread.current[:tilt_blk] = blk
      #{code}
    end
  end

  definer do |*a|
    if blk = Thread.current[:tilt_blk]
      blk.call(*a)
    else
      raise LocalJumpError, "no block given"
    end
  end

  class << self; undef definer end
RUBY

@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