# Std. IO streams in Ruby

**URL:** <https://www.ruby-forum.com/t/std-io-streams-in-ruby/197143>\
**Category:** Rails\
**Created:** [August 1, 2010, 8:29pm UTC](https://www.ruby-forum.com/t/std-io-streams-in-ruby/197143 "2010-08-01T20:29:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![silver-spoon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aeb1de/32.png) [@silver-spoon](https://www.ruby-forum.com/u/silver-spoon)\
**Post date:** [August 1, 2010, 8:29pm UTC](https://www.ruby-forum.com/t/std-io-streams-in-ruby/197143/1 "2010-08-01T20:29:57Z")

</div>

In my rails controller, I am doing the following :

@output = `g++ j.cpp -o "prog" && ./prog`

This gives the output in the @output variable which i can display in my  
view. But the above works only if the j.cpp is correct and doesn’t  
expect any user input. How can I use the stdin/stderr and stdout streams  
here so 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.  
If there are any errors in the file, then I should be able to get the  
errors and display them to the user.  
I tried doing this :

@output = `g++ j.cpp -o "prog" && ./prog| tee prog`

This allows me to enter the user input at the server command prompt (the  
server log…i don’t know what do we call it), but I want it to be  
entered in a textfield in the view.

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 and then i can show the user a text field and get the user input  
to be passed in the stdin stream.  
Let me know if there’s a doubt in the question.

---

<div class="post-metadata">

**Author:** ![silver-spoon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aeb1de/32.png) [@silver-spoon](https://www.ruby-forum.com/u/silver-spoon)\
**Post date:** [August 1, 2010, 9:32pm UTC](https://www.ruby-forum.com/t/std-io-streams-in-ruby/197143/2 "2010-08-01T21:32:02Z")

</div>

On Aug 1, 7:29 pm, Jatin K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> textfield where he/she can enter the input, and the program continues to
> 
> 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 and then i can show the user a text field and get the user input  
> to be passed in the stdin stream.  
> Let me know if there’s a doubt in the question.

Well in a plain old ruby script IO.popen allows you to run a command  
and gets you the command stdin/stdout (and open3/popen3 gets you  
stderr too).

Controlling this interactively via a webapp is much more tricky. A  
single http request only has a single response, so you’d have to break  
this up across multiple requests. There’s no guarantee that the next  
request from the user will go to the same rails instance, so figuring  
out what do do with the IO objects connecting you to the command being  
run is tricky enough.

One possible way out is to have some long running daemon process that  
does that actual running of the command and have your rails actions  
communicate with that. If just polling for whether the command is  
waiting for input isn’t acceptable then you might need to look at  
comet and that sort of thing. If you can restrict yourself to specific  
browsers, the new html5 websockets stuff might be interesting too

Fred

---

<div class="post-metadata">

**Author:** ![silver-spoon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aeb1de/32.png) [@silver-spoon](https://www.ruby-forum.com/u/silver-spoon)\
**Post date:** [August 1, 2010, 10:09pm UTC](https://www.ruby-forum.com/t/std-io-streams-in-ruby/197143/3 "2010-08-01T22:09:37Z")

</div>

Frederick C. wrote:

> On Aug 1, 7:29ï¿½pm, Jatin K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > textfield where he/she can enter the input, and the program continues to
> > 
> > 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 and then i can show the user a text field and get the user input  
> > to be passed in the stdin stream.  
> > Let me know if there’s a doubt in the question.
> 
> Well in a plain old ruby script IO.popen allows you to run a command  
> and gets you the command stdin/stdout (and open3/popen3 gets you  
> stderr too).
> 
> Controlling this interactively via a webapp is much more tricky. A  
> single http request only has a single response, so you’d have to break  
> this up across multiple requests. There’s no guarantee that the next  
> request from the user will go to the same rails instance, so figuring  
> out what do do with the IO objects connecting you to the command being  
> run is tricky enough.
> 
> One possible way out is to have some long running daemon process that  
> does that actual running of the command and have your rails actions  
> communicate with that. If just polling for whether the command is  
> waiting for input isn’t acceptable then you might need to look at  
> comet and that sort of thing. If you can restrict yourself to specific  
> browsers, the new html5 websockets stuff might be interesting too
> 
> Fred

@Fred :-I have been looking over the internet for a solution but never  
got a real answer. Thanks for your help.  
My web app allows the user to run code in the browser window itself, so  
do i really need to do polling for that? I mean is there any better  
solution? If not, how can I use your approach. I have never tried  
something like that before, so a little confused. Can you explain it  
using a little code snippet? That would really help me out.  
Also, the responses would be through AJAX. The user code will be sent  
back to server through an AJAX call, the file created for the code and  
then the response back to the browser(the output of file executed) in an  
AJAX call. So, that guarantee that next request will go to same rails  
instance, won’t that vanish away or not? Could be elaborate a little bit  
on this too.

---

<div class="post-metadata">

**Author:** ![silver-spoon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aeb1de/32.png) [@silver-spoon](https://www.ruby-forum.com/u/silver-spoon)\
**Post date:** [August 2, 2010, 1:34am UTC](https://www.ruby-forum.com/t/std-io-streams-in-ruby/197143/4 "2010-08-02T01:34:22Z")

</div>

On Aug 1, 9:09 pm, Jatin K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> something like that before, so a little confused. Can you explain it  
> using a little code snippet? That would really help me out.

Don’t have any code snippets, just one way I’d try tackling this - run  
the commands from a separate ruby process and use whatever mechanism  
you want (drb might be handy) to allow your controller action to get  
from this process what the state of the command being run is.

> Also, the responses would be through AJAX. The user code will be sent  
> back to server through an AJAX call, the file created for the code and  
> then the response back to the browser(the output of file executed) in an  
> AJAX call. So, that guarantee that next request will go to same rails  
> instance, won’t that vanish away or not? Could be elaborate a little bit  
> on this too.

Unless I’ve misunderstood your command needs to keep running over the  
course of several requests if you’re going to be getting input from  
the users at the point. Unless you’re willing for your rails code to  
just block until input is required ( not a great idea) you’ll need  
some form of polling/comet.
