# Is this possible?

**URL:** <https://www.ruby-forum.com/t/is-this-possible/76875>\
**Category:** Ruby\
**Created:** [November 21, 2006, 10:06pm UTC](https://www.ruby-forum.com/t/is-this-possible/76875 "2006-11-21T22:06:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jspencer78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jspencer78](https://www.ruby-forum.com/u/jspencer78)\
**Post date:** [November 21, 2006, 10:06pm UTC](https://www.ruby-forum.com/t/is-this-possible/76875/1 "2006-11-21T22:06:52Z")

</div>

Is there anyway to override the STDIN of a ruby program, do that when a  
command such as ‘gets’ is called somewhere randomly in the program, I am  
able to control what action is taken. i.e. i don’t want it to block if  
there is nothing in the STDIN buffer, i would like to be able to do my  
own thing.

NOTE: the programs will be generated randomly, so i can’t just call a  
different method (one i defined). But i can add to the code…

Thanks for your time

---

<div class="post-metadata">

**Author:** ![jspencer78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jspencer78](https://www.ruby-forum.com/u/jspencer78)\
**Post date:** [November 21, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/is-this-possible/76875/2 "2006-11-21T22:17:05Z")

</div>

On Wed, 2006-11-22 at 06:06 +0900, James S. wrote:

> Is there anyway to override the STDIN of a ruby program, do that when a  
> command such as ‘gets’ is called somewhere randomly in the program, I am  
> able to control what action is taken. i.e. i don’t want it to block if  
> there is nothing in the STDIN buffer, i would like to be able to do my  
> own thing.
> 
> NOTE: the programs will be generated randomly, so i can’t just call a  
> different method (one i defined). But i can add to the code…

You can use IO#reopen on STDIN, or you can undefine the constant and  
then redefine it.

Aria

---

<div class="post-metadata">

**Author:** ![jspencer78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jspencer78](https://www.ruby-forum.com/u/jspencer78)\
**Post date:** [November 21, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/is-this-possible/76875/3 "2006-11-21T22:35:09Z")

</div>

On Nov 21, 2006, at 1:16 PM, Aredridel wrote:

> > NOTE: the programs will be generated randomly, so i can’t just call a  
> > different method (one i defined). But i can add to the code…
> 
> You can use IO#reopen on STDIN, or you can undefine the constant and  
> then redefine it.

Please don’t mess with STDIN, STDOUT and STDERR unless you really,  
really have to.

Use $std\* instead.

[http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout](http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout)

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![jspencer78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jspencer78](https://www.ruby-forum.com/u/jspencer78)\
**Post date:** [November 21, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/is-this-possible/76875/4 "2006-11-21T22:36:36Z")

</div>

Aredridel wrote:

> On Wed, 2006-11-22 at 06:06 +0900, James S. wrote:
> 
> > Is there anyway to override the STDIN of a ruby program, do that when a  
> > command such as ‘gets’ is called somewhere randomly in the program, I am  
> > able to control what action is taken. i.e. i don’t want it to block if  
> > there is nothing in the STDIN buffer, i would like to be able to do my  
> > own thing.
> > 
> > NOTE: the programs will be generated randomly, so i can’t just call a  
> > different method (one i defined). But i can add to the code…
> 
> You can use IO#reopen on STDIN, or you can undefine the constant and  
> then redefine it.
> 
> Aria

Do you think you could expand? (i am fairly new to Ruby) what actually  
happens when, for example, ‘gets’ is called? Does it just try and read  
from the STDIN variable and then block if this is equal to ‘nil’ ?
