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

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Marc
  • 41
  • 1

4 Answers4

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.

Dino Viehland
  • 6,478
  • 20
  • 25
0

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

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • 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 '10 at 05: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.

Yuliy
  • 17,381
  • 6
  • 41
  • 47
-1

I think that this post answers your question.

Community
  • 1
  • 1
Ikaso
  • 2,268
  • 19
  • 26
  • Your post is about Process and ProcessStartInfo, not about IronRuby which the OP asked. – JJS Mar 07 '18 at 20:14