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
精彩评论