views:

50

answers:

2

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 process 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

+1  A: 

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.

Reese Moore
Thanks Resse, I maybe should have explained myself better, I'm writing an application that is going to fire off commands to several servers and listen for responses, I was wondering if I could do each of these as a separate process - as the system i'm running on is going to have many cores.
MattyW
A: 

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

luiscolorado