From: Jatin Kumar Date: 2010-08-03T21:56:48+09:00 Subject: Re: Standard IO streams in Ruby >> In my rails controller, I am doing the following : >> >> @output = `g++ j.cpp -o "prog" && ./prog` > > Why are you compiling it every time? I am not compiling it every time, only when the user clicks on the compile/execute button that i provide in the view. > 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? Yes, I am exactly doing that. Why does it sound to you insanely dangerous?? Could you elaborate a little on that.? >> 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 > Yes, i know that I have to capture IO for that. Thanks for the link. Really appreciated. > 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. Yes, I am using AJAX. I will look into Comet or Web Sockets, but I have no idea of a web socket and how do they work? How can I use them in here? >> 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 > No, I am not overwriting the program with the output. There's probably something wrong but i can take care of it. >> 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. Got your point. > 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/ Thanks for the links. I will read them all and will get back to you if i need help. > There are probably newer/better alternatives to those, too... Newer / Better alternatives ??? I could not think of a better way. If you have something in your mind that can ease the above process, that would be great. Thanks for all your help. -- Posted via http://www.ruby-forum.com/.