Skip to content

Instantly share code, notes, and snippets.

@pquimo
Last active December 11, 2015 04:19
Show Gist options
  • Save pquimo/4544613 to your computer and use it in GitHub Desktop.
Save pquimo/4544613 to your computer and use it in GitHub Desktop.
approximate ruby equivalent of C++ local object destructors to guarantee cleanup
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