From ec4333c9703939109588af87936ee43ad48f9dcc Mon Sep 17 00:00:00 2001 From: Pierre Merlin Date: Fri, 15 Mar 2024 14:23:50 +0100 Subject: [PATCH] [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 --- doc/syntax/exceptions.rdoc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/doc/syntax/exceptions.rdoc b/doc/syntax/exceptions.rdoc index 31e2f0175c..ac5ff78a95 100644 --- a/doc/syntax/exceptions.rdoc +++ b/doc/syntax/exceptions.rdoc @@ -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.