[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Is this possible?

James Smith

11/21/2006 9:07:00 PM

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

--
Posted via http://www.ruby-....

3 Answers

Aria Stewart

11/21/2006 9:16:00 PM

0

On Wed, 2006-11-22 at 06:06 +0900, James Smith 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


Eric Hodel

11/21/2006 9:33:00 PM

0

On Nov 21, 2006, at 1:16 PM, Aredridel wrote:
> On Wed, 2006-11-22 at 06:06 +0900, James Smith 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.

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

Use $std* instead.

http://blog.se.../articles/2006/08/17/stdout...

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...



James Smith

11/21/2006 9:37:00 PM

0

Aredridel wrote:
> On Wed, 2006-11-22 at 06:06 +0900, James Smith 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' ?


--
Posted via http://www.ruby-....