From: James Smith Date: 2006-11-10T19:28:08+09:00 Subject: Re: Two way communication with the command shell (IO.popen?) unknown wrote: > On Wed, 8 Nov 2006, James Smith wrote: > >> Thank you very much for your comments. I am basically writing a web >> application where users can write and save ruby programs; then run them on >> the server. I am at the stage where the user can save their programs onto >> the server, however i now need to be able to input and recieve output from >> them, as well as error messages. I am fairly certain i will need to create a >> new thread for each of these processes, and probably have a timeout on each >> of them. > > ah. you really need to do be able to do this then > >> Any help on this would be very much appreciated. Are there any sources that >> you'd recommend me looking at? > > sure, i think this already does everything you need: > > harp:~ > cat a.rb > require 'open4' > > def gen_ruby_program path = 'b.rb' > open path, 'w' do |f| > f.write <<-src > puts "what's your name?" > name = gets > puts("hi %s!" % name) > src > end > path > end > > program = gen_ruby_program > > stdin, stdout, stderr = "zaphod", "", "" > timeout = 42 > > status = > open4.spawn "ruby #{ program }", :stdin=>stdin, :stdout=>stdout, > :stderr=>stderr, :timeout=>timeout > > puts "status: #{ status.inspect }" > puts "stdout: #{ stdout.inspect }" > puts "stderr: #{ stderr.inspect }" > harp:~ > ruby a.rb > status: # > stdout: "what's your name?\nhi zaphod!\n" > stderr: "" > > > if you are on windows you'll want to use systemu instead of open4. > > -a Hello again, Thanks once again for your comments. OK i started off by installing 'systemu' (i am on windows). But i am struggling to understand your code, and i can't work out if the external ruby program is included or not. What i would like is for the user program to be completely seperate from the rails, so the user doesn't know anything about the rails program. Obviously when writing the users ruby program to file i can add some lines of code if necessary. Then i would just like the rails program to execute the users program (using its own thread??). This will mean it reading any output from the users program and if it is waiting for input, notify the rails program (eventually i will now display a dialog allowing the user to input) and the rails program writes to it. Then exit the thread after a timeout (if necessary) and print any errors. So i just it to behave as though i was running the program on the command line. If this is wat the code above does, then could you please just explain it to me please. Thank you once again. jamie -- Posted via http://www.ruby-forum.com/.