I would expect it to put --- at the end of each line. Instead it replaces the first 3 chars of each line with "---"
···
---
---ging nha-a30p-009.nha.corp [127.0.0.1] with 32 bytes of data:
---
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---
---g statistics for 127.0.0.1:
--- Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
---roximate round trip times in milli-seconds:
--- Minimum = 0ms, Maximum = 0ms, Average = 0ms
What am I doing wrong? And why?
Thanks.
Best regards.
philip
----
break; /* don't do magic till later */
-- Larry Wall in stab.c from the perl source code
---
#puts automatically places a \n at the end of the line, and it's
better to use interpolation. The reason that I switched from -e '...'
to -e "..." is because I tested it on Windows.
-austin
···
On Sat, 3 Jul 2004 01:13:17 +0900, Philip Mateescu <pmateescu@novosadhayes.com> wrote:
I would expect it to put --- at the end of each line. Instead it replaces the first 3 chars of each line with "---"
[snip]
What am I doing wrong? And why?
Probably line endings - windows line endings are "\r\n" - and chomp! seems to only be removing
the "\n", so the carriage return is returning the cursor to the beginning of the line before
trying to display the "---\n" string.
Try using line.rstrip! to remove all trailing whitespace, or line.chomp!("\r\n").
Unless you want to process the entire output of the ping command at once, using IO#each_line
might be better than IO#readlines. Also, the "\n" at the end of the string isn't needed -
IO#puts is capable of adding one automatically.
You're one windows, so the line ending coming out of ping is \r\n, not
\n -- chomp! isn't eating that, and leaving the carriage-return, but not
the linefeed. So it prints the whole line, returns to the start, prints
three dashes, then newlines and starts again.
Take two characters off the end instead of one and it should work.
Ari
···
On Sat, 2004-07-03 at 01:13 +0900, Philip Mateescu wrote:
Hello,
I'm having problems with an one-liner (ruby 1.6.8 (2003-02-25)
[i386-cygwin]).
I would expect it to put --- at the end of each line. Instead it
replaces the first 3 chars of each line with "---"
---
---ging nha-a30p-009.nha.corp [127.0.0.1] with 32 bytes of data:
---
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---
---g statistics for 127.0.0.1:
--- Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
---roximate round trip times in milli-seconds:
--- Minimum = 0ms, Maximum = 0ms, Average = 0ms
The real reason has been adequately addressed in the rest of this
thread.
If you want #chomp to remove the line-ending, you must chomp with $/,
which is the input record separator ($/ is cross-platform, so your
one-liner will work anywhere):
Note that you don't have to use $< with -e, because readlines is a
Kernel method that reads from $< by default (I don't even know what $<
is).
Note that I used %!...! for the interpolated string to get around the
stupid windows shell. You can use just about any character after the %,
except for a few special ones ([qQsSwW] I think). (I typically use ! or
^.)
Note that I also used string interpolation, %!#{...}--!, rather than
storing to a temporary and using string concatenation. String
interpolation is both faster to type and faster to interpret.
To find out what all the special global variables are ($<, $:, $/, etc)
see English.rb in your ruby library directory.
···
Philip Mateescu (pmateescu@novosadhayes.com) wrote:
Hello,
I'm having problems with an one-liner (ruby 1.6.8 (2003-02-25)
[i386-cygwin]).
#puts automatically places a \n at the end of the line, and it's
better to use interpolation. The reason that I switched from -e '...'
to -e "..." is because I tested it on Windows.
-austin
Best regards.
philip
----
Lightning flashes,
Sparks shower,
In one blink of your eyes,
You have missed seeing.
I would expect it to put --- at the end of each line. Instead it replaces the first 3 chars of each line with "---"
---
---ging nha-a30p-009.nha.corp [127.0.0.1] with 32 bytes of data:
---
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---
---g statistics for 127.0.0.1:
--- Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
---roximate round trip times in milli-seconds:
--- Minimum = 0ms, Maximum = 0ms, Average = 0ms
What am I doing wrong? And why?
You're one windows, so the line ending coming out of ping is \r\n, not
\n -- chomp! isn't eating that, and leaving the carriage-return, but not
the linefeed. So it prints the whole line, returns to the start, prints
three dashes, then newlines and starts again.
Take two characters off the end instead of one and it should work.
Ari
Best regards.
philip
----
All I ask is a chance to prove that money can't make me happy.
---
Yes, I found that one out the hard way too (totally mystified me).
martin
···
Austin Ziegler <halostatue@gmail.com> wrote:
#puts automatically places a \n at the end of the line, and it's
better to use interpolation. The reason that I switched from -e '...'
to -e "..." is because I tested it on Windows.
Just remember what \r does. In your original code, if \r appeared at the end
of each line, the line would print, then \r would return the cursor to the
beginning of the same line, then --- would print, then \n would cause a
newline break.