class Pipe

Pipe class of the pipe-run. Currently with one static method only.

Pipe class of the pipe-run. Currently with one static method only.

Public Class Methods

run(command, &block) click to toggle source

Runs the command and returns its standard output.

If block is given, treat call as non-blocking. In that case, loads em-pipe-run and expects EventMachine loaded.

@param [String] command command for run @param [Proc] block block for giving back the results @yield [String] command output @return [String] command output

# File lib/pipe-run.rb, line 22
def self.run(command, &block)
    if not block.nil?
        begin
            return self.run_nonblock(command, &block)
        rescue NoMethodError
            require "em-pipe-run"
            retry
        end
    end
    
    ###
    
    pipe = File.popen(command, "r")
    
    result = pipe.read
    pipe.close()
    
    return result
end
run2(command, &block) click to toggle source

Runs the command and returns both standard output and error output.

If block is given, treat call as non-blocking. In that case, loads em-pipe-run and expects EventMachine loaded.

@param [String] command command for run @param [Proc] block block for giving back the results @return [Array] command output and error output @since 0.3.0

# File lib/pipe-run.rb, line 55
def self.run2(command, &block)
    if not block.nil?
        begin
            return self.run_nonblock2(command, &block)
        rescue NoMethodError
            require "em-pipe-run"
            retry
        end
    end
    
    ###
    
    begin
        stdin, stdout, stderr = Open3.popen3(command)
    rescue NameError
        require "open3"
        retry
    end
    
    stdin.close()
    outvalue = stdout.read
    stdout.close()
    errvalue = stderr.read
    stderr.close()
    
    return [outvalue, errvalue]
end
run_nonblock(command, &block) click to toggle source

Runs the command and yields its standard output.

@param [String] command command for run @param [Proc] block callback for giving back the results @yield [String] command standard output @since 0.2.0

# File lib/em-pipe-run.rb, line 76
def self.run_nonblock(command, &block)
    pipe = File.popen(command, "r")
    EM::attach(pipe, Receiver, block)
end
run_nonblock2(command) { |outval, errval| ... } click to toggle source

Runs the command and yields both its standard output and error output.

@param [String] command command for run @param [Proc] block callback for giving back the results @yield [String] command standard output @yield [String] command erroroutput @since 0.3.0

# File lib/em-pipe-run.rb, line 92
def self.run_nonblock2(command, &block)
    begin
        stdin, stdout, stderr = Open3.popen3(command)
    rescue NameError
        require "open3"
        retry
    end
    
    outval = nil
    errval = nil
    
    yielder = Proc::new do
        if (not outval.nil?) and (not errval.nil?)
            yield outval, errval
        end 
    end
            
    outcall = Proc::new do |_outval|
         outval = _outval
         yielder.call()
    end
    
    errcall = Proc::new do |_errval|
         errval = _errval
         yielder.call()
    end
    
    EM::attach(stdout, Receiver, outcall)
    EM::attach(stderr, Receiver, errcall)
end