From: "list. rb" Date: 2009-06-20T06:28:30+09:00 Subject: Re: Capturing System Call Return Values? --0016e644b91e61a259046cba34c0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Fri, Jun 19, 2009 at 5:26 PM, list. rb wrote: > class Task > attr_accessor :cmd, :pid, :output, :exitstatus, :thread > def initialize(cmd) > @cmd =3D cmd > queue =3D Queue.new > @thread =3D Thread.new(queue) {|q| > pipe =3D IO.popen(cmd + " 2>&1") > q.push(pipe) > q.push(pipe.pid) > self.pid =3D pipe.pid > begin > self.output =3D pipe.readlines > pipe.close > self.exitstatus =3D $?.exitstatus > rescue =3D> e > q.push e > end > } > queue.clear > end > def join > thread.join > end > end > > task =3D Task.new("ruby -e 'sleep 3 and puts Time.now'") > task.join > p task > > On Fri, Jun 19, 2009 at 4:05 PM, Robert Klemme > wrote: > > > On 19.06.2009 20:52, Tyler Knappe wrote: > > > >> Roger Pack wrote: > >> > >>> Tyler Knappe wrote: > >>> > >>>> I'm attempting to use Ruby to make system calls (in this case python > >>>> scripts) which return values, > >>>> > >>> > > The term "system call" is usually used to denote calls of functions in > the > > system library (e.g. open(), read(), fork()). What you mean would rath= er > be > > called an "external process". (Of course that's done via a system call > as > > well, but this is just one of a multitude of available system calls.) > > > > that I then hand off to Ruby on Rails to > >>>> populate a webpage. However, I've run into a problem with capturing > the > >>>> return values of system calls. > >>>> > >>> appears to be $?, from some google work > >>> > >>> > http://74.125.155.132/search?q=3Dcache:lNLxDxyXbj4J:www.rubyist.net/~slag= ell/ruby/globalvars.html+ruby+global+variables&cd=3D1&hl=3Den&ct=3Dclnk&gl= =3Dus&client=3Dfirefox-a > < > http://74.125.155.132/search?q=3Dcache:lNLxDxyXbj4J:www.rubyist.net/%7Esl= agell/ruby/globalvars.html+ruby+global+variables&cd=3D1&hl=3Den&ct=3Dclnk&g= l=3Dus&client=3Dfirefox-a > > > >>> > >> > >> $? exit status of last executed child process > >> > >> I don't think is quite correct. I tried it and was returned a value o= f > 0. > >> > > > > Why would that mean that the statement above is not correct? > > > > I've found a work around by splitting the returned string using > >> [string].split(' ') but this isn't as elegant as I had hoped. > >> > > > > Now I am not sure what you want: are you interested in the exit status = of > a > > process that was executed? E.g. > > > > irb(main):010:0> system "false" > > =3D> false > > irb(main):011:0> $? > > =3D> # > > irb(main):012:0> system "echo", "test" > > test > > =3D> true > > irb(main):013:0> $? > > =3D> # > > irb(main):014:0> > > > > Or do you mean the output of an external process, e.g. > > > > irb(main):001:0> dir =3D `ls -dl` > > =3D> "drwxrwxrwt+ 2 robert Benutzer 0 Jun 19 21:53 .\n" > > irb(main):002:0> > > > > Your last statement seems to indicate the latter. > > > > Kind regards > > > > robert > > > > -- > > remember.guy do |as, often| as.you_can - without end > > http://blog.rubybestpractices.com/ > > > > > Whoops, I top posted class Task attr_accessor :cmd, :pid, :output, :exitstatus, :thread def initialize(cmd) @cmd =3D cmd queue =3D Queue.new @thread =3D Thread.new(queue) {|q| pipe =3D IO.popen(cmd + " 2>&1") q.push(pipe) q.push(pipe.pid) self.pid =3D pipe.pid begin self.output =3D pipe.readlines pipe.close self.exitstatus =3D $?.exitstatus rescue =3D> e q.push e end } queue.clear end def join thread.join end end task =3D Task.new("ruby -e 'sleep 3 and puts Time.now'") task.join p task --0016e644b91e61a259046cba34c0--