[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Capturing the output of an external program line by line

Aditya Mahajan

10/14/2007 7:24:00 PM

4 Answers

Markus Schirp

10/14/2007 7:29:00 PM

0

Am Mon, 15 Oct 2007 04:23:41 +0900
schrieb Aditya Mahajan <adityam@umich.edu>:

> Hi,
>
> I want to run an external program in ruby. The external program takes
> a long time to execute. While running, it gives the information on
> stdout about what it is doing. How can I run the external program
> inside ruby so that I can capture this information, and display it on
> stdout? I tried %x{...}, `...` and system("...."), but all of them
> give me a result AFTER the external program has finished executing,
> not during its execution.
>
> Thanks,
> Aditya
>

in,out = IO.popen($command)

in refers to standart input of the program
out to the standart output

you can receive lines from the program using in.gets. Beware, "in" may
be a reserved word in ruby.

Tim Hunter

10/14/2007 7:39:00 PM

0

Aditya Mahajan wrote:
> Hi,
>
> I want to run an external program in ruby. The external program takes a
> long time to execute. While running, it gives the information on stdout
> about what it is doing. How can I run the external program inside ruby
> so that I can capture this information, and display it on stdout? I
> tried %x{...}, `...` and system("...."), but all of them give me a
> result AFTER the external program has finished executing, not during its
> execution.
>
> Thanks,
> Aditya
>
I've been using this:

IO.popen("#{cmd} 2>&1") do |f|
while line = f.gets do
# do whatever you want with line
end
end

Note that this gets both stdout and stderr.

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]

Aditya Mahajan

10/14/2007 7:51:00 PM

0

Tim Hunter wrote:
> Aditya Mahajan wrote:
>> Thanks,
>> Aditya
>>
> I've been using this:
>
> IO.popen("#{cmd} 2>&1") do |f|
> while line = f.gets do
> # do whatever you want with line
> end
> end
>
> Note that this gets both stdout and stderr.

Thank you Tim and Markus. Both your methods work perfectly. Since I just
needed to capture the program's output, I am using Tim's method.

Thanks,
Aditya
--
Posted via http://www.ruby-....

mortee

10/14/2007 11:28:00 PM

0