1

Let's say I have a process started from a ruby script like so:

pid = spawn('./my_awesome_process')
Process.detach(pid)

This process listens on STDIN. I now wish to read command line input, and send it to the process like so:

puts "Please enter input"
input = gets

How do I then forwards input to my Process?

Abraham P
  • 15,029
  • 13
  • 58
  • 126

1 Answers1

3

You can use IO.popen, which attaches the subprocess's stdin and stdout to an IO object.

IO.popen('rev', 'r+') do |io|
  io.puts "I'm looking at the man in the mirror"
  io.close_write
  puts io.gets
end
rorrim eht ni nam eht ta gnikool m'I
Kristján
  • 18,165
  • 5
  • 50
  • 62