Capture standard output from IronRuby Script using the DLR API

I have a very simple test.rb file:

puts "Hello World" 

I want to execute this file in C #, for example:

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

How can I capture "Hello World"?

+4
source share
4 answers

ScriptRuntime has an IO property that returns a ScriptIO object. You can call SetOutput and redirect the output. As already mentioned, there is also Console.SetOut, which you might want to call, calls the user directly calls Console.WriteLine. It's nice to use ScriptIO, although you can have multiple scripts in different ScriptRuntime scripts for different outputs.

+3
source

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

+1
source

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

0
source

I think this answer answers your question.

-1
source

Source: https://habr.com/ru/post/1304050/


All Articles