[DOC] exceptions.rdoc : Add heads up about ensure not returning implicitly

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
This commit is contained in:
Pierre Merlin 2024-03-15 14:23:50 +01:00 committed by GitHub
parent 077ac25ed8
commit ec4333c970
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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:
@ -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.