Created
October 7, 2010 17:46
-
-
Save trshafer/615523 to your computer and use it in GitHub Desktop.
Differences with ensure and return
This file contains 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
# Returns the expected method return | |
def return_out_of_ensure | |
return 'actual return' | |
ensure | |
'not returned' | |
end | |
# Returns from ensure despite a method return | |
def return_within_ensure | |
return 'not returned' | |
ensure | |
return 'actual return' | |
end | |
# This just raises an error | |
def raise_and_no_return_witin_ensure | |
raise 'will actually raise an error' | |
ensure | |
'not returned' | |
end | |
# This returns a value and raises an error | |
def raise_and_return_witin_ensure | |
raise 'will actually raise an error' | |
ensure | |
return 'actual return' | |
end | |
puts return_out_of_ensure | |
puts return_within_ensure | |
begin | |
puts raise_and_no_return_witin_ensure | |
rescue Exception => e | |
puts e.inspect | |
end | |
# Same method in two execution scenarios | |
# Although this raises an error, it cannot be rescued into a variable | |
begin | |
puts raise_and_return_witin_ensure | |
rescue Exception => e | |
puts e.inspect | |
end | |
puts raise_and_return_witin_ensure |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment