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"?

4 Answers 4

2

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.

0

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

1
  • 1
    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
    Mar 14, 2010 at 5:04
0

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.

-1

I think that this post answers your question.

1
  • Your post is about Process and ProcessStartInfo, not about IronRuby which the OP asked.
    – JJS
    Mar 7, 2018 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.