Ruby child process output stream

I want to be able to pass the output of a child process in Ruby

eg.

p `ping google.com` 

I want to see ping answers immediately; I do not want to wait for the process to complete.

+6
source share
3 answers

You should use IO # popen :

 IO.popen("ping -c 3 google.com") do |data| while line = data.gets puts line end end 
+5
source

Instead of using backlinks, you can do the following:

 IO.popen('ping google.com') do |io| io.each { |s| print s } end 

Hurrah!

+9
source

If you want to capture both 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 
+3
source

Source: https://habr.com/ru/post/905128/


All Articles