[#104004] [Ruby master Feature#17883] Load bundler/setup earlier to make `bundle exec ruby -r` respect Gemfile — mame@...
Issue #17883 has been reported by mame (Yusuke Endoh).
21 messages
2021/05/24
[ruby-core:104064] [Ruby master Feature#15567] Allow ensure to match specific situations
From:
merch-redmine@...
Date:
2021-05-27 13:38:22 UTC
List:
ruby-core #104064
Issue #15567 has been updated by jeremyevans0 (Jeremy Evans).
ioquatix (Samuel Williams) wrote in #note-14:
> Here is working example:
>
> ~~~ ruby
> def transaction
> failed = false
>
> begin
> puts "Begin Transaction"
> yield
> rescue Exception
> puts "Abort Transaction"
> failed = true
> raise
> ensure
> puts "Commit Transaction" unless failed
> end
> end
>
> catch(:ball) do
> begin
> raise "Problem"
> rescue
> transaction do
> throw :ball
> end
> end
> end
> ~~~
>
> This seems overly complex to me personally.
You can simplify this slightly using a local variable:
```ruby
def transaction
failed = false
begin
puts "Begin Transaction"
yield
rescue Exception => exc
puts "Abort Transaction"
raise
ensure
puts "Commit Transaction" unless exc
end
end
catch(:ball) do
begin
raise "Problem"
rescue
transaction do
throw :ball
end
end
end
```
I think such code makes sense. Exceptions abort the transaction, but should be reraised and not swallowed, so the `raise` doesn't feel out of place.
Looking at my earlier example:
```ruby
def doot
ret = yield
normal_exit = true
ret
ensure
# Did the block return normally
return "abnormal" if $! || !normal_exit
end
```
This is a case that wouldn't work correctly in the transaction scenario, where it is called inside an existing `rescue` block. So you would currently have to use:
```ruby
def doot
ret = yield
normal_exit = true
ret
rescue Exception => exc
raise
ensure
# Did the block return normally
return "abnormal" if exc || !normal_exit
end
```
About the only way I can think to make that easier would be for `ensure` to support `=>`:
```ruby
def doot
ret = yield
normal_exit = true
ret
ensure => exc
# Did the block return normally
return "abnormal" if exc || !normal_exit
end
```
In this case `exc` would be `nil` if the code before ensure did not raise an exception, and the exception instance if it did.
However, I think the cases where this actually matters (`ensure` without `rescue` but that cares about whether an exception has been raised) are so few that we shouldn't consider language modifications to support them.
----------------------------------------
Feature #15567: Allow ensure to match specific situations
https://bugs.ruby-lang.org/issues/15567#change-92238
* Author: ioquatix (Samuel Williams)
* Status: Rejected
* Priority: Normal
* Assignee: ioquatix (Samuel Williams)
----------------------------------------
There are some situations where `rescue Exception` or `ensure` are not sufficient to correctly, efficiently and easily handle abnormal flow control.
Take the following program for example:
```
def doot
yield
ensure
# Did the function run to completion?
return "abnormal" if $!
end
puts doot{throw :foo}
puts doot{raise "Boom"}
puts doot{"Hello World"}
catch(:foo) do
puts doot{throw :foo}
end
```
Using `rescue Exception` is not sufficient as it is not invoked by `throw`.
Using `ensure` is inefficient because it's triggered every time, even though exceptional case might never happen or happen very infrequently.
I propose some way to limit the scope of the ensure block:
```
def doot
yield
ensure when raise, throw
return "abnormal"
end
```
The scope should be one (or more) of `raise`, `throw`, `return`, `next`, `break`, `redo`, `retry` (everything in `enum ruby_tag_type` except all except for `RUBY_TAG_FATAL`).
Additionally, it might be nice to support the inverted pattern, i.e.
```
def doot
yield
ensure when not return
return "abnormal"
end
```
Inverted patterns allow user to specify the behaviour without having problems if future scopes are introduced.
`return` in this case matches both explicit and implicit.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>