On Thu, 2 Mar 2006, Phrogz wrote:

> That's awesome, ara. Unfortunately, I need to write something that will
> work on random MacOS machines witj a default Ruby install. Can I poke
> about in the innards of session and steal ideas/code?

absolutely.  basically what you're after is open3 - it's in the stdlib and
will probably suffice.  session is overkill if you're not running multiple
commands per session (no pun intended) anyhow.

anyhow, here's the essence:

     harp:~ > cat a.rb
     def spawn command, opts = {}
       require 'open3'
       stdin = opts.values_at(:stdin, 'stdin', 0).compact.first
       stdout = opts.values_at(:stdout, 'stdout', 1).compact.first
       stderr = opts.values_at(:stderr, 'stderr', 2).compact.first

       Open3::popen3(command) do |i,o,e|
         i << stdin if stdin
         i.close # important!
         o.each{|line| stdout << line} if stdout
         e.each{|line| stderr << wrine} if stderr
       end

       $?.exitstatus
     end

     stdout, stderr = '', ''
     exitstatus = spawn 'cat', 0=>42, 1=>stdout, 2=>stderr

     require 'yaml'
     y 'exitstatus' => exitstatus,
       'stdout'     => stdout,
       'stderr'     => stderr


     harp:~ > ruby a.rb
     ---
     stdout: "42"
     stderr: ""
     exitstatus: 0


regards.

-a

-- 
judge your success by what you had to give up in order to get it.
- h.h. the 14th dali lama