From: David Masover Date: 2010-08-03T11:58:00+09:00 Subject: Re: Standard IO streams in Ruby On Sunday, August 01, 2010 01:06:59 pm Jatin Kumar wrote: > In my rails controller, I am doing the following : > > @output = `g++ j.cpp -o "prog" && ./prog` Why are you compiling it every time? From some of your questions, it seems like you're going to let the user enter source code, then compile it and run it. Is that true? If so, do you realize how insanely dangerous that is? > If the user has to give input, I open a dialog box on the view with a > textfield where he/she can enter the input, and the program continues to > execute. You can't do that from inside a Rails controller. You can capture IO with popen or open3: http://www.ensta.fr/~diam/ruby/online/ruby-doc- stdlib/libdoc/open3/rdoc/classes/Open3.html But that doesn't solve your problem by itself -- you need to send data to and from the browser interactively. That means you at least need AJAX, and possibly Comet or Web Sockets, combined with some sort of background processing. > I tried doing this : > > @output = `g++ j.cpp -o "prog" && ./prog| tee prog` That would overwrite your program with the output of your program. Is that really what you want to do? man tee > In short, how can I get control of stdin, so that whenever the stdin > waits for some user input, i get to know that it is waiting for the user > input You can't do that either, not really. The best you can do is always provide that text field, and show the user standard output and standard error, so they can see the program prompting them -- and detect the program exiting, so you can remove the text field. If you want to be able to do this efficiently, you're going to need to do a lot of reading. It basically boils down to these two options: http://en.wikipedia.org/wiki/Comet_(programming) http://www.websockets.org/ But however you implement the updates -- you could always just poll -- but you're still going to need one of these: http://www.tobinharris.com/past/2009/3/9/6-ways-to-run-background-jobs-in- rubyonrails/ There are probably newer/better alternatives to those, too...