# Replacing StandardInput for ScriptEngine?

**URL:** <https://www.ruby-forum.com/t/replacing-standardinput-for-scriptengine/178521>\
**Category:** IronRuby\
**Created:** [November 5, 2009, 9:30pm UTC](https://www.ruby-forum.com/t/replacing-standardinput-for-scriptengine/178521 "2009-11-05T21:30:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![confundido](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/90ced4/32.png) [@confundido](https://www.ruby-forum.com/u/confundido)\
**Post date:** [November 5, 2009, 9:30pm UTC](https://www.ruby-forum.com/t/replacing-standardinput-for-scriptengine/178521/1 "2009-11-05T21:30:41Z")

</div>

Hello,

I am experimenting with script IO in preparation for hosting an IRB  
session via an embedded SSH server and/or embedded HTTP server, but I’m  
having trouble getting the script environment to use a custom stream for  
standard input. I used the following code for setting up my IronRuby  
environment:

public Form1()

```
    {

        InitializeComponent();

        _stdIn = new MemoryStream();

        _inputWriter = new StreamWriter(_stdIn);

        txtStandardIn.KeyPress+=(s,e)=>

                                    {

```

\_inputWriter.Write(e.KeyChar);

```
                                        _inputWriter.Flush();

                                        Debug.Write(e.KeyChar);

                                    };

        _stdOut = new FifoStream();

        _outputReader = new StreamReader(_stdOut);

        _stdOut.DataArrived += StdOutDataArrived;

        _scriptEngine = Ruby.CreateEngine();

        _globalScope = _scriptEngine.CreateScope();

        _scriptEngine.Runtime.IO.SetInput(_stdIn, Encoding.Default);

        _scriptEngine.Runtime.IO.SetOutput(_stdOut,

```

Encoding.Default);

```
    }

```

If I write “hello” + Environment.NewLine to the input stream and flush  
the writer and then use the ScriptEngine e to execute a “x = gets” - x  
is always nil. However, if I use the script engine to execute “puts  
‘hello’” my custom output stream is used and I see my output.

I feel like I am missing a key ingredient in my setup process - any help  
would be appreciated.

Thanks,

Nathan

---

<div class="post-metadata">

**Author:** ![confundido](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/90ced4/32.png) [@confundido](https://www.ruby-forum.com/u/confundido)\
**Post date:** [November 5, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/replacing-standardinput-for-scriptengine/178521/2 "2009-11-05T23:40:40Z")

</div>

Please disregard. The StandardInput is being set properly and IronRuby  
is using it. Writing to a MemoryStream (obviously) positions the stream  
at EOF - the next read will obviously come up empty unless the position  
is reset. Used a queue-based stream and all worked well.

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Nathan Stults  
Sent: Thursday, November 05, 2009 12:30 PM  
To: removed\_email\_address@domain.invalid  
Subject: [Ironruby-core] Replacing StandardInput for ScriptEngine?

Hello,

I am experimenting with script IO in preparation for hosting an IRB  
session via an embedded SSH server and/or embedded HTTP server, but I’m  
having trouble getting the script environment to use a custom stream for  
standard input. I used the following code for setting up my IronRuby  
environment:

public Form1()

```
    {

        InitializeComponent();

        _stdIn = new MemoryStream();

        _inputWriter = new StreamWriter(_stdIn);

        txtStandardIn.KeyPress+=(s,e)=>

                                    {

```

\_inputWriter.Write(e.KeyChar);

```
                                        _inputWriter.Flush();

                                        Debug.Write(e.KeyChar);

                                    };

        _stdOut = new FifoStream();

        _outputReader = new StreamReader(_stdOut);

        _stdOut.DataArrived += StdOutDataArrived;

        _scriptEngine = Ruby.CreateEngine();

        _globalScope = _scriptEngine.CreateScope();

        _scriptEngine.Runtime.IO.SetInput(_stdIn, Encoding.Default);

        _scriptEngine.Runtime.IO.SetOutput(_stdOut,

```

Encoding.Default);

```
    }

```

If I write “hello” + Environment.NewLine to the input stream and flush  
the writer and then use the ScriptEngine e to execute a “x = gets” - x  
is always nil. However, if I use the script engine to execute “puts  
‘hello’” my custom output stream is used and I see my output.

I feel like I am missing a key ingredient in my setup process - any help  
would be appreciated.

Thanks,

Nathan
