Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I capture STDOUT to a string?

Tags:

ruby

stdio

puts "hi" puts "bye" 

I want to store the STDOUT of the code so far (in this case hi \nbye into a variable say 'result' and print it )

puts result 

The reason I am doing this is I have integrate an R code into my Ruby code, output of which is given to the STDOUT as the R code runs , but the ouput cannot be accessed inside the code to do some evaluations. Sorry if this is confusing. So the "puts result" line should give me hi and bye.

like image 493
script_kiddie Avatar asked Feb 20 '13 18:02

script_kiddie


People also ask

How do you capture stdout?

To capture a tool's standard output stream, add the stdout field with the name of the file where the output stream should go. Then add type: stdout on the corresponding output parameter.

How do you get stdout in python?

To capture stdout output from a Python function call, we can use the redirect_stdout function. to call redirect_stdout with the f StringIO object. Then we call do_something which prints stuff to stdout. And then we get the value printed to stdout with f.

How do I send a stdout to a file?

Redirecting stdout and stderr to a file: The I/O streams can be redirected by putting the n> operator in use, where n is the file descriptor number. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator. We have created a file named “sample.

How do I redirect to stdout?

The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR, you have to specify 2> for the redirection symbol.


2 Answers

A handy function for capturing stdout into a string...

The following method is a handy general purpose tool to capture stdout and return it as a string. (I use this frequently in unit tests where I want to verify something printed to stdout.) Note especially the use of the ensure clause to restore $stdout (and avoid astonishment):

def with_captured_stdout   original_stdout = $stdout  # capture previous value of $stdout   $stdout = StringIO.new     # assign a string buffer to $stdout   yield                      # perform the body of the user code   $stdout.string             # return the contents of the string buffer ensure   $stdout = original_stdout  # restore $stdout to its previous value end 

So, for example:

>> str = with_captured_stdout { puts "hi"; puts "bye"} => "hi\nbye\n" >> print str hi bye => nil 
like image 167
fearless_fool Avatar answered Sep 21 '22 10:09

fearless_fool


Redirect Standard Output to a StringIO Object

You can certainly redirect standard output to a variable. For example:

# Set up standard output as a StringIO object. foo = StringIO.new $stdout = foo  # Send some text to $stdout. puts 'hi' puts 'bye'  # Access the data written to standard output. $stdout.string # => "hi\nbye\n"  # Send your captured output to the original output stream. STDOUT.puts $stdout.string 

In practice, this is probably not a great idea, but at least now you know it's possible.

like image 45
Todd A. Jacobs Avatar answered Sep 24 '22 10:09

Todd A. Jacobs