Skip to content

Instantly share code, notes, and snippets.

@trshafer
Created October 7, 2010 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trshafer/615523 to your computer and use it in GitHub Desktop.
Save trshafer/615523 to your computer and use it in GitHub Desktop.
Differences with ensure and return
# 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