views:

28

answers:

1

Hi,

I'm building an IronRuby Console in silverlight 4 and WinForms (net4). I can redirect the output without problems:

MyRuntime = Ruby.CreateRuntime();
msOutput = new MemoryStream();
MyRuntime.IO.SetOutput(msOutput, Encoding.UTF8);
MyEngine = MyRuntime.GetEngine("rb");
MySource = MyEngine.CreateScriptSourceFromString("a='123'\nputs a", SourceCodeKind.Statements);
MySource.Execute();
textBox2.Text = ReadFromStream(msOutput);

Now, I want to redirect the input also, but always getting a 'nil' from the script:

MyRuntime = Ruby.CreateRuntime();
msOutput = new MemoryStream();
msInput = new MemoryStream();
MyRuntime.IO.SetOutput(msOutput, Encoding.UTF8);
MyRuntime.IO.SetInput(msInput, Encoding.UTF8);
MyEngine = MyRuntime.GetEngine("rb");
MySource = MyEngine.CreateScriptSourceFromString("a=gets\nputs a", SourceCodeKind.Statements);
byte[] byteArray = Encoding.UTF8.GetBytes("123");
msInput.Write(byteArray, 0, byteArray.Length);
MySource.Execute();
textBox2.Text = ReadFromStream(msOutput);

I cannot find any samples of redirecting the input, can you please send an example? Thank you.

A: 

I don't have any sample code immediately available but instead of using a MemoryStream you need to implement the stream. When reads on the stream occur you need to send the "contents" of the text box to the stream. You'll need some mechanism for determining when you send the contents - e.g. when the user hits return. You'll also probably need to setup a thread for blocking for the reads and probably use an AutoResetEvent to block until the text box signals that the input is complete.

Dino Viehland