From: Eric Hodel Date: 2004-07-03T03:54:42+09:00 Subject: Re: Help with one-liner --qse+WBH4guesipZ+ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Philip Mateescu (pmateescu@novosadhayes.com) wrote: > Hello, >=20 > I'm having problems with an one-liner (ruby 1.6.8 (2003-02-25)=20 > [i386-cygwin]). >=20 > My one liner sample: >=20 > $ ping localhost | ruby -e '$<.readlines.each { |line| line.chomp!();=20 > puts(line + "---\n"); }' >=20 > I would expect it to put --- at the end of each line. Instead it=20 > replaces the first 3 chars of each line with "---" >=20 > What am I doing wrong? And why? :) 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): ruby -e "readlines.each {|l| puts %!#{l.chomp $/}!}" 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. --=20 Eric Hodel - drbrain@segment7.net - http://segment7.net All messages signed with fingerprint: FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 --qse+WBH4guesipZ+ Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (FreeBSD) iD8DBQFA5a9wMypVHHlsnwQRAuKdAKCRtoCqBp28IE3Za3kogr6DXeCDqgCbBMP8 y5q0okbLjrvu3TZKVjsrHew= =LvtF -----END PGP SIGNATURE----- --qse+WBH4guesipZ+--