From: "Erik Bågfors" Date: 2001-11-05T04:52:18+09:00 Subject: [ruby-talk:24342] Re: block/yield puzzle On Sun, 2001-11-04 at 20:36, David Alan Black wrote: > Hello -- > > Well, not puzzle in the fun sense -- just something I don't > understand. > > I've been playing around with ways to generated a default block to > yield to. This isn't a particular great way, but I'm stuck on why it > doesn't at least work. > > def thing(x, b = proc { puts "default" } ) > if block_given? > yield > else > thing(x) &b > end > end > > thing(1) { puts "associated block" } # => "associated block" > thing(1) # infinite loop > > > Somehow the "x" argument seems to be causing trouble, because this > version does what I expected: > > def thing(b = proc { puts "default" } ) > if block_given? > yield > else > thing &b > end > end > > thing{ puts "associated block" } # => "associated block" > thing # => "default" > > I'm not seeing why the first version hangs. Enlightenment welcome. > Is there anything wrong with this solution?? def thing(x, b = proc { puts "default" } ) if block_given? yield else thing(x, &b) end end thing(1) { puts "associated block" } # => "associated block" thing(1) # => "default" Works for me. /Erik -- Erik B�gfors | erik@bagfors.nu Supporter of free software | GSM +46 733 279 273 fingerprint: 6666 A85B 95D3 D26B 296B 6C60 4F32 2C0B 693D 6E32