From: Robert Klemme Date: 2010-10-23T15:50:14+09:00 Subject: Re: Why no yield in 1.9.2 blocks? On 22.10.2010 22:09, Ed Howland wrote: > I searched in vain for the answer, but why doesn't yield work inside > Ruby 192 blocks. The syntax allows you to pass the blocks as a&blk > param, but you have to call the block with blk.call. If you try to use > yield, you get a LocalJumpError: This isn't something new in 1.9.2. There is no version of Ruby that I am aware of that would allow this. > Why would you want to pass a block to a block? Why not? so you can do > things like: > > def meth(x,&block) > if block_given? > block.call(x) do |y| > puts "in meth " + y.to_s > end > end > end > > ruby-1.9.2-p0> meth(1) {|x,&blk| puts "in blok " + x.to_s; blk.call 2} > in blok 1 > in meth 2 There are also other useful usecases for this, namely defining methods with a block dynamically # in some class define_method :foo do |x,y,&b| puts "before" yield # or b.call puts "after" end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/