开发者

Spawning Ruby programs from Ruby

Say I have a ruby script called hello_world.rb that has one line

puts "Hello, world"

And I want to call that from another ruby script called spawn_hello_world.rb

pipe = IO.popen("ruby1.9.1 hello_world.rb", 'w+')
if pipe
  puts pipe.gets
end

My question is: Is there a shorthand way of running another ruby p开发者_JAVA技巧rocess without having to call the OS in this way?

I'm aware I could do

pipe = IO.popen('-', 'w+')

which would start another ruby interpreter and I could then send it commands using

pipe.puts "puts "Hello World""

But this seems quite inelegant as well.

I'm basically looking for a ruby equivalent to python's multiprocessing module


You could use eval on the code in the context of it's own binding. This would allow you to execute arbitrary code while still encapsulating your program from the nasty side effects of that code.
It's not quite running another Ruby interpreter, but it will execute your ruby code for you.


Could you please give us some context here? What do you intend to do? Can't you just refactor your code to use the "require" or "include" methods?

Just curious.

Luis

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜