Can Ruby access output from shell commands as it appears? Can Ruby access output from shell commands as it appears? ruby ruby

Can Ruby access output from shell commands as it appears?


You are looking for pipes. Here is an example:

# This example runs the netstat command via a pipe# and processes the data in Ruby as it come backpipe = IO.popen("netstat 3")while (line = pipe.gets)  print line  print "and"end


When call methods/functions to run system/shell commands, your interpreter spawns another process to run it and waits for it to finish, then gives you the output.

Even if you use threads, the only thing that you would accomplish is not letting your program to hang while the command is run, but you still won't get the output till its done.

I think you can accomplish that with pipes, but I am not sure how.

@Marcel got it.