Skip to content

Commit

Permalink
[DOC] exceptions.rdoc : Add heads up about ensure not returning impli…
Browse files Browse the repository at this point in the history
…citly

There is a weird gotcha I already forgot twice.... and regret not to have found in doc.
See https://dev.to/okuramasafumi/be-sure-ensure-doesn-t-return-value-implicitly-8gp
  • Loading branch information
pimpin committed Mar 15, 2024
1 parent 077ac25 commit ec4333c
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions doc/syntax/exceptions.rdoc
Expand Up @@ -86,7 +86,7 @@ To always run some code whether an exception was raised or not, use +ensure+:
rescue
# ...
ensure
# this always runs
# this always runs BUT does not implicitly return the last evaluated statement.
end

You may also run some code when an exception is not raised:
Expand All @@ -96,7 +96,11 @@ You may also run some code when an exception is not raised:
rescue
# ...
else
# this runs only when no exception was raised
# this runs only when no exception was raised AND return the last evaluated statement
ensure
# ...
# this always runs.
# It is evaluated after the evaluation of either the `rescue` or the `else` block.
# It will not return implicitly.
end

NB : Without explicit +return+ in the +ensure+ block, +begin+/+end+ block will return the last evaluated statement before entering in the `ensure` block.

0 comments on commit ec4333c

Please sign in to comment.