On Thu, 6 Jan 2005 14:08:03 -0600, Sam Stephenson <sstephenson / gmail.com> 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