2 ms·
What happens when you return from a proc when the enclosing function has already returned?
by kubb 1y ago
What happens when you return from a proc when the enclosing function has already returned?
- lcnPylGDnU4H9OF 1y agoYou can't call return from inside a Proc inside a method; you will encounter a LocalJumpError.
- vidarh 1y agoYou can call return from inside a proc all you want. You can't do so if you return the proc to an outside scope without getting LocalJumpError. E.g, given: def foo = proc do 42 end def bar = proc do return 42 end Then `foo.call` is fine, but `bar.call` will indeed give a LocalJumpError as you say. But a return in a proc that hasn't escaped its defining scope is fine: def baz = proc do return 42 end.call Calling `baz` here will just return 42 to the surrounding scope.
- vidarh 1y agoThis example: def foo proc do return 42 end.call end def bar proc do return 42 end end p foo p bar.call Will produce: 42 test.rb:11:in `block in bar': unexpected return (LocalJumpError) from test.rb:16:in `<main>' So in other words, it's handled.