You can really use injectto create nested lambdas or procs that you can call at the end. You need your given block to be internal in the socket, so you change your array and use this block as the initial value, and then wrap each subsequent function around the result from the injection:
def in_nested_contexts(&blk)
[:with_this, :with_that, :and_in_this_context].reverse.inject(blk) {|block, symbol|
->{ send symbol, &block }
}.call
end
If you complete your methods with_this, et al with the before and after instructions puts, you can see this in action:
in_nested_contexts { puts "hello, world" }
with_this start
with_that start
context start
hello, world
context end
with_that end
with_this end
source
share