From: shyouhei@... Date: 2017-09-26T08:31:21+00:00 Subject: [ruby-core:83018] [Ruby trunk Feature#13923] Idiom to release resources safely, with less indentations Issue #13923 has been updated by shyouhei (Shyouhei Urabe). tagomoris (Satoshi TAGOMORI) wrote: > I prefer to get resources at once, but #7 looks to work well for my use case. > `Deferred` is ok for me too. > Can we expect that it's bundled as standard library in ruby? No, no plan to bundle it yet. These days it's getting hard to include a new standard library unless there are very good reason for it to avoid being a normal gem. If you think it's worth, please add a separate feature request describing that reason. Thank you in advance. ---------------------------------------- Feature #13923: Idiom to release resources safely, with less indentations https://bugs.ruby-lang.org/issues/13923#change-66930 * 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: