Feature #21822
openExpose Return Value in the ensure Block
Description
I'd like to propose a simple feature that allows easy access to the return value inside an ensure block.
begin
# ...
ensure => ret
# ret is nil if an exception is raised
LOGGER.debug "return value: #{ret}"
end
Updated by mame (Yusuke Endoh) about 1 month ago
- Related to Feature #18083: Capture error in ensure block. added
Updated by nobu (Nobuyoshi Nakada) about 1 month ago
Is the variable nil if an exception is raised, even for non-local variable?
By analogy with rescue, it feels natural for me that the variable is unchanged in that case.
Updated by matz (Yukihiro Matsumoto) 11 days ago
I don't think it's not worth for the new syntax, where you can do similar thing with:
begin
ret = begin
# ....
end
ensure
LOGGER.debug "return value: #{ret}"
end
Matz.
Updated by artemb (Artem Borodkin) 11 days ago
begin ret = begin # .... end ensure LOGGER.debug "return value: #{ret}" end
For me, ensure => ret looks cleaner, reduces cognitive overhead, and is simply easier on the eyes when reviewing real code.
The structure is easier to parse visually, and the intent is clearer compared to the alternatives.
Also, it doesn’t break any existing behavior — it’s purely additive, just a small but useful extension that makes the code more elegant and pleasant to read.