From: shyouhei@... Date: 2017-09-25T12:49:49+00:00 Subject: [ruby-core:82996] [Ruby trunk Feature#13923][Feedback] Idiom to release resources safely, with less indentations Issue #13923 has been updated by shyouhei (Shyouhei Urabe). Status changed from Open to Feedback We looked at this issue in a developer meeting today and nobu's library solution written in comment #7 was popular there. Is that OK for you, or you still want a new syntax than a library (and if that is the case; can you tell us why)? ---------------------------------------- Feature #13923: Idiom to release resources safely, with less indentations https://bugs.ruby-lang.org/issues/13923#change-66904 * Author: tagomoris (Satoshi TAGOMORI) * Status: Feedback * Priority: Normal * Assignee: * Target version: ---------------------------------------- In programs which grabs and releases resources very often, we need to write so much begin-ensure clauses. ```ruby begin storage = getStorage() begin buffer = storage.get(buffer_id) # ... ensure buffer.close if buffer end rescue StorageError => e # ... ensure storage.close if storage end ``` Such code makes our code fat, and difficult to understand. I want to write such code like below: ```ruby # Class of storage and buffer should include a module (like Closeable) # or be checked with respond_to?(:close) begin(storage = getStorage(); buffer = storage.get(buffer_id) # ... rescue StorageError => e # ... end # (buffer.close if buffer) rescue nil # (storage.close if storage) rescue nil ``` Other languages also have similar features: * Java: try-with-resources * Python: with -- https://bugs.ruby-lang.org/ Unsubscribe: