mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Replace The Current Process With An External Command as a ruby til.
This commit is contained in:
@@ -131,6 +131,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
||||
- [Percent Notation](ruby/percent-notation.md)
|
||||
- [Question Mark Operator](ruby/question-mark-operator.md)
|
||||
- [Rake Only Lists Tasks With Descriptions](ruby/rake-only-lists-tasks-with-descriptions.md)
|
||||
- [Replace The Current Process With An External Command](ruby/replace-the-current-process-with-an-external-command.md)
|
||||
- [Squeeze Out The Extra Space](ruby/squeeze-out-the-extra-space.md)
|
||||
- [Summing Collections](ruby/summing-collections.md)
|
||||
- [Uncaught Exceptions In Pry](ruby/uncaught-exceptions-in-pry.md)
|
||||
|
||||
29
ruby/replace-the-current-process-with-an-external-command.md
Normal file
29
ruby/replace-the-current-process-with-an-external-command.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# Replace The Current Process With An External Command
|
||||
|
||||
Ruby's
|
||||
[`Kernel#exec`](http://ruby-doc.org/core-2.2.3/Kernel.html#method-i-exec)
|
||||
method can be used to run an external command. What differentiates it from
|
||||
executing commands with the likes of back ticks or `%x[]` is that instead of
|
||||
forking a child process, it replaces the current process.
|
||||
|
||||
For instance, the following ruby script, when executed, will replace itself
|
||||
with an `irb` session.
|
||||
|
||||
```ruby
|
||||
Kernel.exec('irb')
|
||||
```
|
||||
|
||||
The external command will even benefit from the existing environment. For
|
||||
example, if I set the following environment variable
|
||||
|
||||
```bash
|
||||
$ export GREETING=hello
|
||||
```
|
||||
|
||||
and then execute a file containing
|
||||
|
||||
```ruby
|
||||
Kernel.exec('echo $GREETING')
|
||||
```
|
||||
|
||||
I can expect to see `hello` printed to stdout.
|
||||
Reference in New Issue
Block a user