On Tue, May 22, 2012 at 7:12 AM, David Madison <[email protected]> wrote:
> 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
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
-- You received this message because you are subscribed to the Google Groups
ruby-talk-google group. To post to this group, send email to
[email protected]. To unsubscribe from this group, send email
to [email protected]. For more options, visit this
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en