You can use something similar to Instance eval with delegation pattern, used - for example - in Savon gem:
def batman(task, &block)
@original_self = eval('self', block.binding)
puts "Batman: #{task} - done"
instance_exec('feed cat', &block)
@news << task
end
private
def method_missing(method, *args, &block)
if @original_self
@original_self.send(method, *args, &block)
else
super
end
end
In this approach, when you call method (with implicit receiver) inside block passed into batman
method, it's called in the context of SuperHeros
instance. If there is no such method available, the call goes (through method_missing
) to original block self
.