How to iterate over popen output?

ruby

Solution

The Ruby `IO#gets` method just returns the next line of input from the IO object.

In order to get all of the lines you could call "gets" repeatedly until it's nil, or call `read` to get the entire string, or `readlines` to get the lines of input in an array.

IO.popen('ls -l','r') { |f| puts $_ while f.gets }
IO.popen('ls -l','r') { |f| puts f.read }
IO.popen('ls -l','r') { |f| puts f.readlines }

Problem

I have this simple ruby script: ``` redcricket@dev-006:~$ cat simple.rb #!/usr/local/bin/ruby IO.popen 'ls -l', 'r+' do |f| puts f.gets end ``` and when I run it the only output I get is this ... ``` redcricket@dev-006:~$ ./simple.rb total 32 ``` ... what I expected was this ... ``` redcricket@smp-mig-dev-006:~$ ls -l total 32 drwxr-xr-x 4 redcricket co 4096 Dec 5 12:23 applications -rw-r--r-- 1 redcricket co 464 Oct 5 16:23 config drwxr-xr-x 72 redcricket co 4096 Dec 5 15:11 docs drwxr-xr-x 3 root root 4096 Dec 5 12:14 oradiag_root drwxr-xr-x 5 redcricket co 4096 Dec 5 16:22 platform -rwxr-xr-x 1 redcricket co 373 Dec 5 16:30 process_yum_output.rb -rwxr-xr-x 1 redcricket co 2159 Nov 28 16:24 SetupSSHPK.sh -rwxr-xr-x 1 redcricket co 142 Dec 5 16:31 simple.rb ``` ... I guess I need to iterate over f.gets somehow? Thanks!

Original source