Reputation: 6250
I want to be able to stream the output of a child process in Ruby
e.g.
p `ping google.com`
I want to see the ping responses immediately; I don't want to wait for the process to complete.
Upvotes: 7
Views: 2189
Reputation: 56595
If you'd like to capture both the stdout
and stderr
you can use popen2e
:
require 'open3'
Open3.popen2e('do something') do |_stdin, stdout_err, _wait_thr|
stdout_err.each { |line| puts line }
end
Upvotes: 4
Reputation: 80041
You can do the following instead of using backticks:
IO.popen('ping google.com') do |io|
io.each { |s| print s }
end
Cheers!
Upvotes: 9
Reputation: 124419
You should use IO#popen:
IO.popen("ping -c 3 google.com") do |data|
while line = data.gets
puts line
end
end
Upvotes: 6