Running another ruby script from a ruby script

后端 未结 7 959
终归单人心
终归单人心 2020-12-01 04:48

In ruby, is it possible to specify to call another ruby script using the same ruby interpreter as the original script is being run by?

For example, if a.rb runs b.rb

相关标签:
7条回答
  • 2020-12-01 04:54

    Avdi Grimm wrote a series of articles on the Devver blog about different ways to start Ruby subprocesses last summer:

    • A Dozen (or so) Ways to Start Subprocesses in Ruby: Part 1
    • A Dozen (or so) Ways to Start Subprocesses in Ruby: Part 2
    • A Dozen (or so) Ways to Start Subprocesses in Ruby: Part 3
    • Beware of pipe duplication in subprocesses

    [Note: it appears that part 4 hasn't been published yet.]

    0 讨论(0)
  • 2020-12-01 04:54

    This is what I have used

    /myapp/script/main_script.rb


    load rails env, only if you need to

    ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'development'
    require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
    

    run other slave scripts from within main_script.rb

    require File.expand_path(File.dirname(__FILE__) + "/../script/populate_wh_grape_varieties_table.rb")
    
    0 讨论(0)
  • 2020-12-01 04:57

    Charles Nutter, of JRuby fame, is suggesting a Kernel#ruby method to call a Ruby script using the same Ruby implementation as you're currently using.

    Edit: the proposal was rejected. Matz said that MVM (multiple virtual machines) may provide the solution.

    0 讨论(0)
  • 2020-12-01 05:02

    If you just want to run a script in the context of an existing process, you can also do this

    eval File.read("/path/to/your/script.rb")
    

    Not sure what your use case is but this could be useful for example if you have a Rails console open and you want to execute some code in a scratch file, but don't want to have to keep copying the entire block of code into your console.

    0 讨论(0)
  • 2020-12-01 05:04

    The require trick is a good idea, assuming the script in question doesn't choke trying to redefine any constants you may have set, or calling methods on objects you may have runtime monkey patched to no longer honor their standard contracts.

    In either case, the problem is less the approach than it is the code in the scripts themselves. Show good manners, put your constants in a namespace, and don't monkey patch the runtime desctructively.

    To ensure the script in question doesn't mess with the runtime of your calling script, and to guard against the chance it might call Kernel/Process.exit() somewhere, try the following

    pid=Process.fork do
        require 'script.rb'
        Process.exit
    end
    ignored, status = Process.waitpid2(pid, Process::WNOHANG)
    puts "script.rb PID #{pid} exited, exit status: #{status.exitstatus}" 
    

    For more advanced things like writing to its stdin stream or reading from its stdout or stderr streams, use the Open4 gem.

    0 讨论(0)
  • 2020-12-01 05:05

    http://en.wikibooks.org/wiki/Ruby_Programming/Running_Multiple_Processes might help

    0 讨论(0)
提交回复
热议问题