[ruby-core:82906] Re: [Ruby trunk Feature#13923] Idiom to release resources safely, with less indentations

From: Eric Wong <normalperson@...>
Date: 2017-09-20 20:03:13 UTC
List: ruby-core #82906
matz@ruby-lang.org wrote:
> I like `defer` idea, although adding a new keyword is hard.

I like `defer' idea, too; but I think we can overload `ensure'
keyword to take a block and avoid conflicts:

  def foo
    resource = Resource.acquire
    ensure { resource&.close }
    do_stuff(resource)

    # resource closed by previous ensure block
  end

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next