Today I Learned

hashrocket A Hashrocket project

Replace The Current Process With An External Cmd

Ruby's Kernel#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.


The external command will even benefit from the existing environment. For example, if I set the following environment variable

$ export GREETING=hello

and then execute a file containing

Kernel.exec('echo $GREETING')

I can expect to see hello printed to stdout.

See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.