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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

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.
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.

Comments

-1

I think that this post answers your question.

1 Comment

Your post is about Process and ProcessStartInfo, not about IronRuby which the OP asked.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.