从 ruby 脚本运行另一个 ruby 脚本 [英] Running another ruby script from a ruby script
问题描述
在 ruby 中,是否可以指定使用与运行原始脚本相同的 ruby 解释器来调用另一个 ruby 脚本?
例如,如果a.rb运行b.rb几次,是否可以替换
system("ruby", "b.rb", "foo", "bar")
类似的东西
run_ruby("b.rb", "foo", "bar")
所以如果你在原始文件上使用 ruby1.9.1 a.rb
,ruby1.9.1
将用于 b.rb,但如果你只使用 ruby a.rb
在原版上,ruby
会用在 b.rb 上吗?
我不想使用shebangs,因为我希望它能够在不同的计算机上运行,其中一些没有/usr/bin/env
.>
我的意思不是 load
或 require
之类的,而是产生新进程(所以我可以使用多个 CPU).
Avdi Grimm 写了一系列文章在 Devver 博客上关于去年夏天启动 Ruby 子进程的不同方法:
- 在 Ruby 中启动子进程的十几种(左右)方法:第 1 部分
- 在 Ruby 中启动子进程的十几种(左右)方法:第 2 部分
- 十几种(左右)在 Ruby 中启动子进程的方法:第 3 部分
- 注意子进程中的管道重复
[注意:似乎第 4 部分尚未发布.]
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 a couple of times, is it possible to replace
system("ruby", "b.rb", "foo", "bar")
with something like
run_ruby("b.rb", "foo", "bar")
so that if you used ruby1.9.1 a.rb
on the original, ruby1.9.1
would be used on b.rb, but if you just used ruby a.rb
on the original, ruby
would be used on b.rb?
I'd prefer not to use shebangs, as I'd like it to be able to run on different computers, some of which don't have /usr/bin/env
.
Edit: I didn't mean load
or require
and the like, but spawning new processes (so I can use multiple CPUs).
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.]
这篇关于从 ruby 脚本运行另一个 ruby 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!