<?xml version="1.0" encoding="UTF-8"?>
<codes type="array">
  <code>
    <code>def self.capture_system(*args)
  args = args.flatten.compact.collect { |arg| arg.to_s }
  return false if args.blank?

  rd, wr = IO.pipe

  pid = fork do
    rd.close
    $stdout.reopen wr
    $stderr.reopen wr
    yield if block_given?
    exec(*args) rescue nil
    exit! 1 # never gets here unless exec threw or failed
  end

  wr.close
  out = String.new
  out &lt;&lt; rd.read until rd.eof?
  Process.wait
  raise unless $?.success?
  out
end
</code>
    <comment>Is there a shorter way to do this? Basically, I don't want anything outputted to the current irb session, but I want the variable 'out' to be the output from the fork. Command 'puts capture_system('which', 'git')' should work at the end of it.</comment>
    <created-at type="datetime">2010-02-03T02:34:27+00:00</created-at>
    <id type="integer">1166</id>
    <language>Ruby</language>
    <permalink>silencing-current-output-but-capture-output-from-another-process</permalink>
    <refactors-count type="integer">0</refactors-count>
    <title>Silencing current output, but capture output from another process</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2010-02-03T02:34:27+00:00</updated-at>
    <user-id type="integer">1914</user-id>
    <user>
      <id type="integer">1914</id>
      <identity-url>http://k776.myopenid.com</identity-url>
      <name>k776.myopenid.com</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">0</refactors-count>
      <website>http://k776.tumblr.com</website>
    </user>
  </code>
</codes>
