I’m working on a ruby script that ultimately starts up a system process that takes quite a while. I need to read from the stderr of this process and react to it depending on what is output.
I’m currently doing it as such:
Open3.popen3(cmd_to_run) do |stdin, stdout, stderr, waitthread|
stderr.each_line do |line|
# look out for specific lines and react to them accordingly
end
end
But I’ve also seen implementations to achieve something similar but doing it with kernel#select:
Open3.popen3(cmd_to_run) do |stdin, stdout, stderr, waitthread|
io = select([stderr], nil, nil, 30)
if io.nil?
log("Command timed out during Kernel#select")
return
end
io[0][0].each_line do |line|
# look out for specific lines and react to them accordingly
end
end
I’ve read the pickaxe description of what select does, but I’m confused as to why I should (of if I should) use it? The first method works just the same.
Probably two reasons:
each_lineIOobject, e. g.io = select([stdout, stderr])and more than one event (e.g. write event or exception too)