Actually, in Ruby we have "ensure"
def foo arg
return true if arg == 42
puts "got past the guard"
raise "blah"
ensure
puts "ensure always"
end
foo(42)
foo(3)
The "ensure" blocks gets executed whether or not you return early, throw exception or return at the end of the block.
Re:Avoid Else, Return Early (2013)
by SideburnsOfDoom on Monday March 26, 2018 @09:12AM
(#16678572)
There are several constructs for it, see also "try ... finally" on C# and Java, and "using" statements in C#.
All make the "goto manual cleanup at end" less necessary, and make early return easier to use.
Re:Avoid Else, Return Early (2013)
by wildmusings on Monday March 26, 2018 @09:37AM
(#16678725)
I should have specified that I was talking about C. You usually don’t need this pattern when you have garbage collection or C++ RAII.
No, it doesn't play well with early return, so avoid mixing them.