Q:

Calling instance methods from within blocks

I tried to implement a class allowing its subclasses to save method blocks for get them executed later. It work's, but it seems I took the wrong way. Have a look at this:
class SuperKlass  
  def self.exec_later(&block)
    @@block_for_later = block
  end

  def exec_now
    return unless @@block_for_later
    @@block_for_later.call
  end
end

class ChildKlass < SuperKlass
  exec_later do
    child_method
  end

  def child_method
    puts "Child method called"
  end
end

test_klass = ChildKlass.new
test_klass.exec_now

If I try to execute this piece of code, the call to child_method in the block results in a method-missing error. Does somebody know what I'm doing wrong, and what's the right way to add functionality like this?

A:

The block exec_later is running in the context of ChildKlass, not the instance test_klass. As child_method is an instance method, it isn't found on ChildKlass.
UPDATE: I found a solution:
  def exec_now
    return unless @@block_for_later
    instance_eval &@@block_for_later
  end

