From: MonkeeSage Date: 2007-12-17T22:25:10+09:00 Subject: Re: Is there any way to pass further the "hidden" block? On Dec 17, 6:52 am, Trans wrote: > On Dec 17, 3:11 am, "Chiyuan Zhang" wrote: > > > > > Like this: > > > def foo > > yield > > end > > > def bar(&brk) > > foo(&brk) > > end > > > bar { puts "foo" } > > > What I want to know is: is there any way to define `bar' like this: > > > def bar > > foo > > end > > > so that foo will get the block passed to bar? I want to know this > > because I learn fromhttp://www.pluralsight.com/blogs/dbox/archive/2006/05/09/23068.aspx > > that the implicit block is much faster than explicitly passing it > > as &brk . I know one solution is > > > def bar > > foo { yield } > > end > > > but that create another block, not the original one. > > Do you mean, is there a keyword #block to go along with #block_given? > and #yield? As far as I know, there is no way to pass the block except > via an explicit reference. > > But I think explicit may be the future. I'm pretty sure I've heard > some chatter about getting rid of #block_given? Technically I would > think it possible to all but eliminate the speed difference between > explicit and implicit --3x seems very high. > > T. On further testing it appears that in situations where can create a proc and reuse it, the performance comes much closer (although the implicit version says the same?!): require "benchmark" def implicit_blk yield "baz" end def explicit_blk(&blk) blk.call("baz") end n = 1_000_000 prc = lambda { | y | y } Benchmark.bm(10) { | x | x.report("implicit") { n.times { implicit_blk(&prc) } } x.report("explicit") { n.times { explicit_blk(&prc) } } } # => user system total real implicit 3.730000 0.520000 4.250000 ( 4.479818) explicit 6.000000 0.580000 6.580000 ( 6.763514) ...wonder why? Regards, Jordan