Issue #15277 has been updated by marcandre (Marc-Andre Lafortune).


normalperson (Eric Wong) wrote:
>  Would this work for subprocesses? (system, spawn, ``, popen, ...)

I'm not sure I understand the question. `exec` effectively terminates the Ruby process although the `at_exit` callbacks are not called. `system`, `spawn`, etc... don't do such a thing and would thus would not call the `at_exec` callbacks.

> Ruby already has a mechanism for hooking existing method calls

Right. I believe that monkey-patching builtin method calls is something that should be highly discouraged though.

>  However, `at_exec` would have the SAME surprises as monkey-patching.

Maybe so. If (god forbid!) there are multiple such monkey-patches, each with their own callback queues and potentially slightly different implementations, than there would be more surprises.

I imagine that the need of calling `at_exec` is rare though, and my goal is not really to minimize surprises. My goal is to make it easier & nicer to get that result, and avoid having monkey patching as the "official solution" to this issue. We can't even prepend a module to intercept the call to `exec`, so we have to do the alias_method chaining.

----------------------------------------
Feature #15277: at_exec
https://bugs.ruby-lang.org/issues/15277#change-74727

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
There's currently no easy way to have code executed before a subsequent call to `exec`. One has to monkey-patch the builtin method.

I'd like to propose a new method `at_exec` that would be very similar to `at_exit`, except that the callbacks are triggered before the current process is replaced by the external command.

```
# This would output "Hello", "Bye", and "Foo"
at_exec { puts "Bye!" }
puts "Hello"
exec "echo Foo"
```

Use case: we roll our own in `DeepCover`. Some test suites will call `exec`, and we need to store our counters before that happens.



-- 
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>