Sha256: 3f919b502843878f8d634a929ea75a39c578c170f39776c2339f51be1ed8d346

Contents?: true

Size: 748 Bytes

Versions: 9

Compression:

Stored size: 748 Bytes

Contents

require 'open3'

module Utils
  class Subprocess
    def initialize(cmd, &block)
      # see: http://stackoverflow.com/a/1162850/83386
      Open3.popen3(cmd) do |stdin, stdout, stderr, thread|
        # read each stream from a new thread
        { :out => stdout, :err => stderr }.each do |key, stream|
          Thread.new do
            until (line = stream.gets).nil? do
              # yield the block depending on the stream
              if key == :out
                yield line, nil, thread if block_given?
              else
                yield nil, line, thread if block_given?
              end
            end
          end
        end

        thread.join # don't exit until the external process is done
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ssh_scan-0.0.44 lib/ssh_scan/subprocess.rb
ssh_scan-0.0.43 lib/ssh_scan/subprocess.rb
ssh_scan-0.0.42 lib/ssh_scan/subprocess.rb
ssh_scan-0.0.41 lib/ssh_scan/subprocess.rb
ssh_scan-0.0.40 lib/ssh_scan/subprocess.rb
ssh_scan-0.0.39 lib/ssh_scan/subprocess.rb
ssh_scan-0.0.38 lib/ssh_scan/subprocess.rb
ssh_scan-0.0.38.pre lib/ssh_scan/subprocess.rb
ssh_scan-0.0.37 lib/ssh_scan/subprocess.rb