From: "ioquatix (Samuel Williams)" Date: 2021-08-21T00:52:26+00:00 Subject: [ruby-core:105036] [Ruby master Feature#18083] Capture error in ensure block. Issue #18083 has been updated by ioquatix (Samuel Williams). > A few places, but in relative usages probably 1/1000 of begin/rescue or ensure/end patterns, right? I scanned top 10,000 gems by download count: Checking for "$!": Found 459 gems with this pattern. Found 2624 instances of the pattern. Checking for "ensure": Found 3221 gems with this pattern. Found 28306 instances of the pattern. Checking for /(.*ensure(.*\n){0,4}.*\$!.*)/: Found 35 gems with potential issues. Found 46 instances of the pattern. Any usage of $! is potentially suspect, but at least 35 gems out of 10,000 are definitely incorrect. ---------------------------------------- Feature #18083: Capture error in ensure block. https://bugs.ruby-lang.org/issues/18083#change-93445 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal ---------------------------------------- As discussed in https://bugs.ruby-lang.org/issues/15567 there are some tricky edge cases. As a general model, something like the following would be incredibly useful: ``` ruby begin ... ensure => error pp "error occurred" if error end ``` Currently you can get similar behaviour like this: ``` ruby begin ... rescue Exception => error raise ensure pp "error occurred" if error end ``` The limitation of this approach is it only works if you don't need any other `rescue` clause. Otherwise, it may not work as expected or require extra care. Also, Rubocop will complain about it. Using `$!` can be buggy if you call some method from `rescue` or `ensure` clause, since it would be set already. It was discussed extensively in https://bugs.ruby-lang.org/issues/15567 if you want more details. -- https://bugs.ruby-lang.org/ Unsubscribe: