Sunday, 15 September 2013

ruby - Wrapping a block in method calls that accept blocks by name -


I have a number of methods that I call like this:

  with_this Do with_that and end_in_hists_context yield end date   

I remember that there was a trick to wrap one such block call again. How can I write a method that makes the block wrapping for me?

  def in_nested_contexts (& amp; blk) references = [: with_this, with_that ,: and_in_this_context] #Window may end in injection    

you can actually inject nested lambdass or procs, which you can finally call you The block requires a nesting interior, so you reverse your array and use that block as the starting value, then the result of each In the Fislkar Wrap:

  def in_nested_contexts (& Amp; blk) [: with_this ,: with_that, myself_END_STRONG_TAG_ in_this_context] .reverse.inject (blk) {| Block, symbol | - & gt; Send a symbol, & amp; Block}} .col   

If you wrap your with_this , then enter before and after statement, you can see it in action:

  in_nested_contexts {puts "hello, world"} # = & gt; Start with start_that start with start_this begin hello, world reference end with_that end with_this    

No comments:

Post a Comment