From: Robert Klemme Date: 2012-08-22T15:37:47+09:00 Subject: Re: Strange line feed behavior with ping command. On Tue, Aug 21, 2012 at 1:56 PM, Antoine P. wrote: > When I try to echo a simple ping localhost with the same script : > I have a different output with annoying added line feeds which > are not visible in a regular command line : This likely has to do with Windows programs using \r\n as line termination. Executing the Windows ping.exe: irb(main):001:0> x=IO.popen ['/cygdrive/c/Windows/system32/ping', 'www.google.de'], 'r' do |io| io.read end => "\r\nPinging www-cctld.l.google.com [173.194.69.94] with 32 bytes of data:\r\nReply from 173.194.69.94: bytes=32 time=39ms TTL=43\r\nReply from 173.194.69.94: bytes=32 time=38ms TTL=43\r\nReply from 173.194.69.94: bytes=32 time=38ms TTL=43\r\nReply from 173.194.69.94: bytes=32 time=40ms TTL=43\r\n\r\nPing statistics for 173.194.69.94:\r\n Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),\r\nApproximate round trip times in milli-seconds:\r\n Minimum = 38ms, Maximum = 40ms, Average = 38ms\r\n" irb(main):002:0> x.each_line.to_a => ["\r\n", "Pinging www-cctld.l.google.com [173.194.69.94] with 32 bytes of data:\r\n", "Reply from 173.194.69.94: bytes=32 time=39ms TTL=43\r\n", "Reply from 173.194.69.94: bytes=32 time=38ms TTL=43\r\n", "Reply from 173.194.69.94: bytes=32 time=38ms TTL=43\r\n", "Reply from 173.194.69.94: bytes=32 time=40ms TTL=43\r\n", "\r\n", "Ping statistics for 173.194.69.94:\r\n", " Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),\r\n", "Approximate round trip times in milli-seconds:\r\n", " Minimum = 38ms, Maximum = 40ms, Average = 38ms\r\n"] irb(main):003:0> x.each_line("\r\n").to_a => ["\r\n", "Pinging www-cctld.l.google.com [173.194.69.94] with 32 bytes of data:\r\n", "Reply from 173.194.69.94: bytes=32 time=39ms TTL=43\r\n", "Reply from 173.194.69.94: bytes=32 time=38ms TTL=43\r\n", "Reply from 173.194.69.94: bytes=32 time=38ms TTL=43\r\n", "Reply from 173.194.69.94: bytes=32 time=40ms TTL=43\r\n", "\r\n", "Ping statistics for 173.194.69.94:\r\n", " Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),\r\n", "Approximate round trip times in milli-seconds:\r\n", " Minimum = 38ms, Maximum = 40ms, Average = 38ms\r\n"] irb(main):004:0> x.each_line("\n").to_a => ["\r\n", "Pinging www-cctld.l.google.com [173.194.69.94] with 32 bytes of data:\r\n", "Reply from 173.194.69.94: bytes=32 time=39ms TTL=43\r\n", "Reply from 173.194.69.94: bytes=32 time=38ms TTL=43\r\n", "Reply from 173.194.69.94: bytes=32 time=38ms TTL=43\r\n", "Reply from 173.194.69.94: bytes=32 time=40ms TTL=43\r\n", "\r\n", "Ping statistics for 173.194.69.94:\r\n", " Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),\r\n", "Approximate round trip times in milli-seconds:\r\n", " Minimum = 38ms, Maximum = 40ms, Average = 38ms\r\n"] irb(main):005:0> x=IO.popen ['/cygdrive/c/Windows/system32/ping', 'www.google.de'], 'r' do |io| io.each {|l| p l} end "\r\n" "Pinging www-cctld.l.google.com [173.194.69.94] with 32 bytes of data:\r\n" "Reply from 173.194.69.94: bytes=32 time=40ms TTL=43\r\n" "Reply from 173.194.69.94: bytes=32 time=40ms TTL=43\r\n" "Reply from 173.194.69.94: bytes=32 time=40ms TTL=43\r\n" "Reply from 173.194.69.94: bytes=32 time=39ms TTL=43\r\n" "\r\n" "Ping statistics for 173.194.69.94:\r\n" " Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),\r\n" "Approximate round trip times in milli-seconds:\r\n" " Minimum = 39ms, Maximum = 40ms, Average = 39ms\r\n" => # Note, this is with 1.9 installed from sources. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/