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"?
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"?
You can redirect standard output and read it in your C# program as shown here.
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.
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.