Last active
August 29, 2015 14:23
-
-
Save chorn/bf0c82058880f2fb1d0a to your computer and use it in GitHub Desktop.
popen4 woo hoo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open4' | |
module Derp | |
def self.system(*args) | |
raise "No arguments specified." unless args && args.class == Array && args.size > 0 | |
# This style of calling system allows for redirection and pipes, where the normal array style does not. | |
command = (args.flatten.compact.map{|a| a.match(/ /) ? "\"#{a}\"" : a }).join(' ') | |
puts command if $DEBUG | |
pid, stdin, stdout, stderr = Open4::popen4 command | |
stdin.close | |
waitpid, status = Process::waitpid2 pid | |
unless status.success? | |
puts "The following system call failed with return code #{status.exitstatus}" | |
puts command | |
puts "pid : #{ pid }" | |
puts "stdout : #{ stdout.read.strip }" | |
puts "stderr : #{ stderr.read.strip }" | |
puts "waitpid : #{ waitpid }" | |
end | |
status | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment