tags:

views:

43

answers:

4

I have a very simple test.rb file:

puts "Hello World"

I want to execute this file within c#, eg:

var runtime = Ruby.CreateRuntime();
runtime.ExecuteFile("C:\test.rb");

How can I capture the "Hello World"?

A: 

You can redirect standard output and read it in your C# program as shown here.

Eric J.
Hi Eric,I actually would like to use the IronRuby Runtime as opposed to as an external process so I can pass variables into and out of the script. The method you link to does not appear to work with that.
Marc
A: 

I think that this post answers your question.

Ikaso
A: 

One thing you can do is to call Console.setOut and/or Console.setErr before the ExecuteFile and again afterward. The first time you will redirect the output to a stream of your choosing, and then restore it to the previous value.

Yuliy
+1  A: 

ScriptRuntime has an IO property which returns a ScriptIO object. You can call SetOutput on that and redirect the output. As others have mentioned there's also Console.SetOut which you might want to call incase the user calls Console.WriteLine directly. The nice thing about using ScriptIO though is you can have multiple scripts in different ScriptRuntime's writing to different outputs.

Dino Viehland