Ruby subprocess

B

badcherry

I am wanting inside of a ruby script to execute another ruby script in
a subprocess in the same manner as exec(). As first thought I
considered seeing if you can lookup the ruby executable path from
within ruby, but I couldn't find a way of doing it. If that was the
case you could just say exec("/usr/bin/ruby " + "file.rb").
 
A

ara.t.howard

I am wanting inside of a ruby script to execute another ruby script in
a subprocess in the same manner as exec(). As first thought I
considered seeing if you can lookup the ruby executable path from
within ruby, but I couldn't find a way of doing it. If that was the
case you could just say exec("/usr/bin/ruby " + "file.rb").

require 'rbconfig'

def which_ruby
c = ::Config::CONFIG
File::join(c['bindir'], c['ruby_install_name']) << c['EXEEXT']
end

ruby = which_ruby

exec ruby, 'file.rb'

probably won't work on windows since the one-click breaks (or used to break)
rbconfig.

-a
 
R

Robert Klemme

I am wanting inside of a ruby script to execute another ruby script in
a subprocess in the same manner as exec(). As first thought I
considered seeing if you can lookup the ruby executable path from
within ruby, but I couldn't find a way of doing it. If that was the
case you could just say exec("/usr/bin/ruby " + "file.rb").

Depending on the platform you can use fork like this:

child_pid = fork do
# child code
load "file.rb"
end
# ...
Process.wait(child_pid)
print "Child ", child_pid, " exited with ", $?.exitstatus, "\n"

See
http://www.ruby-doc.org/core/classes/Kernel.html#M005750
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html

HTH

robert
 
B

badcherry

require 'rbconfig'

def which_ruby
c = ::Config::CONFIG
File::join(c['bindir'], c['ruby_install_name']) << c['EXEEXT']
end

ruby = which_ruby

exec ruby, 'file.rb'

probably won't work on windows since the one-click breaks (or used to break)
rbconfig.

This will work for now, but didn't test it on windows yet. Thanks,
much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,234
Messages
2,571,180
Members
47,813
Latest member
RustyGary3

Latest Threads

Top