Spawn a process and keep the output, but stdout -> $stdout

Hello all.
I basically "want" to run a sub process within ruby, having its stdout
displayed on the screen (as if run by
system("command")
)
however, I want to be able to feed it input, like keeping its stdin
stream around somehow, so I can feed it the character 'q' to tell it to
quit eventually. Is this possibly natively in windows ruby, or do I
need to use win32-process type magic, anybody know (I'm on jruby, btw).
Thanks!
-roger-

···

--
Posted via http://www.ruby-forum.com/.

Ok I figured it out.

basically it's
open("|command name", "w")

then it outputs its standard out to stdout, but accepts stdin from you
(if you have any other ways to effect the same thing, do let me know).
Thanks!
-r

···

--
Posted via http://www.ruby-forum.com/.

IO.popen() and just write program's output to STDOUT yourself?

http://www.ruby-doc.org/core-1.9.3/IO.html#method-c-popen

-- Matma Rex

open("|cmd"..) is the simplest, but if you want a simple way to examine
the output you can use popen and a thread:

bc = IO.popen('bc','r+')

# A thread which just reads output from bc and prints it
bcout = Thread.new {
  while !bc.eof
    puts "BC says: #{bc.gets}"
  end
}

# Now let's control bc:

bc.puts "3*2"
bc.puts "4*8"

sleep 2
bc.puts "41958/999"

# Remember we need to send a quit so that the process ends!
bc.puts "quit"

# Make sure to wait for the bcout read loop to finish before we exit
bcout.join

···

--
Posted via http://www.ruby-forum.com/.

I was thinking of io = IO.popen('process', 'r+'). You can now both
write to and read from io.

-- Matma Rex

open("|cmd"..) is the simplest, but if you want a simple way to examine
the output you can use popen and a thread:

bc = IO.popen('bc','r+')

I'd rather use the block form - as always.

# A thread which just reads output from bc and prints it
bcout = Thread.new {
while !bc.eof
puts "BC says: #{bc.gets}"
end
}

Or simply

bcout = Thread.new { bc.each {|line| puts line} }

or even simpler on 1.9

bcout = Thread.new { IO.copy_stream(bc, $stdout) }

Note, this will likely not get line wise output.

# Now let's control bc:

bc.puts "3*2"
bc.puts "4*8"

sleep 2
bc.puts "41958/999"

# Remember we need to send a quit so that the process ends!
bc.puts "quit"

bc.close_write works as well.

# Make sure to wait for the bcout read loop to finish before we exit
bcout.join

Right.

There are a whole lot of interesting methods in Open3 as well:
http://rdoc.info/stdlib/open3/Open3

Kind regards

robert

···

On Tue, May 22, 2012 at 7:12 AM, David Madison <lists@ruby-forum.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/