Last active
December 11, 2015 04:19
-
-
Save makanimason/4544613 to your computer and use it in GitHub Desktop.
approximate ruby equivalent of C++ local object destructors to guarantee cleanup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def has_return | |
puts "start of has_return" | |
return | |
puts "unreachable code in has_return" | |
ensure | |
puts "cleanup from has_return method" | |
end | |
def has_exception | |
puts "start of has_exception" | |
raise "blah" | |
puts "unreachable code in has_exception" | |
ensure | |
puts "cleanup from has_exception method" | |
end | |
def catch_exception_yes | |
begin | |
puts "start of catch_exception_yes" | |
raise "blah" | |
puts "end of catch_exception_yes" | |
rescue => _ | |
puts "rescue in catch_exception_yes" | |
else | |
puts "else in catch_exception_yes" | |
end | |
end | |
def catch_exception_no | |
begin | |
puts "start of catch_exception_no" | |
puts "end of catch_exception_no" | |
rescue => _ | |
puts "rescue in catch_exception_no" | |
else | |
puts "else in catch_exception_no" | |
end | |
end | |
def has_loop | |
[1,2,3,4].each do |index| | |
begin | |
puts "loop #{index}" | |
break if index == 2 | |
ensure | |
puts "cleanup after each loop iteration" | |
end | |
end | |
end | |
has_return | |
has_loop | |
catch_exception_yes | |
catch_exception_no | |
has_exception |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment