Change the context/binding inside a block in ruby

Solution 1:

Paste this code:

  def evaluate(&block)
    @self_before_instance_eval = eval "self", block.binding
    instance_eval &block
  end

  def method_missing(method, *args, &block)
    @self_before_instance_eval.send method, *args, &block
  end

For more information, refer to this really good article here

Solution 2:

Maybe

def command(*names, &blk)
  command = make_command_object(..)
  command.instance_eval(&blk)
end

can evaluate the block in the context of command object.