From: Sam Stephenson Date: 2005-01-07T05:15:59+09:00 Subject: Re: Return from a block? On Thu, 6 Jan 2005 14:08:03 -0600, Sam Stephenson wrote: > Another way to do it is to rescue LocalJumpError. > > | def yield_with_return_value(*args) > | yield *args > | rescue LocalJumpError => e > | e.exit_value > | end > | > | def foo(&block) > | yield_with_return_value &block > | end > > irb(main):001:0> foo {return "hello"; "this is not returned"} > => "hello" > irb(main):002:0> foo {"bar"} > => "bar" Sorry, I should make it a bit clearer that the block's return value is caught: | def foo(&block) | value = yield_with_return_value &block | "baz " + value | end irb(main):001:0> foo {return "hello"; "this is not returned"} => "baz hello" irb(main):002:0> foo {"bar"} => "baz bar" Sam