SundayMonday
SundayMonday

Reputation: 19727

Store value in stdout from popen3 in Ruby

How can I store the string value in stdout from the following Ruby code?

stdin, stdout, stderr = Open3.popen3('grep something test.txt')

I can display the value like this: stdout.gets. However trying to store the value like this: s = stdout.gets just sets s to nil. Trying to store the value like this: s = stdout stores something like "#<IO:0x1003abe10>" in s.

Upvotes: 1

Views: 848

Answers (2)

Duke Robillard
Duke Robillard

Reputation: 181

Open3.capture3 might be what you want; it gives you strings from STDOUT and STDERR, and the status of the process:

outstr, errstr, status = Open3.capture3('/bin/command-here', param, param2)
logger.info 'output: ' + outstr + '; error: ' + errstr 
             + "; " return code: " + status.exitstatus.to_s

Upvotes: 1

SundayMonday
SundayMonday

Reputation: 19727

output = stdout.read seems to work.

Upvotes: 1

Related Questions