Project

General

Profile

Actions

Feature #21822

open

Expose Return Value in the ensure Block

Feature #21822: Expose Return Value in the ensure Block
1

Added by artemb (Artem Borodkin) about 2 months ago. Updated 11 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:<unknown>]

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

Related issues 1 (1 open0 closed)

Related to Ruby - Feature #18083: Capture error in ensure block.OpenActions

Updated by mame (Yusuke Endoh) about 1 month ago Actions #2

Updated by nobu (Nobuyoshi Nakada) about 1 month ago Actions #3 [ruby-core:124522]

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 Actions #4 [ruby-core:124785]

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 Actions #5 [ruby-core:124803]

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.

Actions

Also available in: PDF Atom