Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Write to stdin and read from stdout?

Tags:

ruby

I am writing a ruby program that is supposed to execute another program, pass values to it via stdin, read the response from its stdout, and then print the response. This is what I have so far.

#!/usr/bin/env ruby

require 'open3'

stdin, stdout, stderr = Open3.popen3('./MyProgram')

stdin.puts "hello world!"

output = stdout.read
errors = stderr.read

stdin.close
stdout.close
stderr.close

puts "Output:"
puts "-------"
puts output
puts "\nErrors:"
puts "-------"
puts errors

I am definitely doing something wrong here - when I run this it seems to be waiting for me to enter text. I don't want to be prompted for anything - I want to start ./MyProgram, pass in "hello world!", get back the response, and print the response on the screen. How do I do this?

EDIT

Just in case it matters, MyProgram is basically a program that keeps running until EOF, reading in and printing out stuff.

like image 260
Kvass Avatar asked Oct 10 '13 18:10

Kvass


1 Answers

Try closing stdin before reading the output. Here's an example:

require 'open3'
Open3.popen3("./MyProgram") do |i, o, e, t|
  i.write "Hello World!"
  i.close
  puts o.read
end

Here's a more succint way of writing it using Open3::capture3: (beware, untested!)

o, e, s= Open3.capture3("./MyProgram", stdin_data: "Hello World!")
puts o
like image 185
nurettin Avatar answered Sep 21 '22 10:09

nurettin