From: Robert Klemme Date: 2013-06-14T20:11:03+09:00 Subject: Re: Unexpected output piping to ruby -pe '' --089e01493bf43a4a3f04df1b4d30 Content-Type: text/plain; charset=ISO-8859-1 On Fri, Jun 14, 2013 at 1:14 AM, Eric Christopherson < echristopherson@gmail.com> wrote: > On Thu, Jun 13, 2013 at 4:49 PM, Greg Ferguson wrote: > >> How come Ruby outputs the incoming piped text when there is no code to >> evaluate: >> >> echo foo | ruby -pe '' >> >> outputs: "foo" >> >> Without "-p" there's no output, which I'd expect. Without '' there's an >> error, which I'd expect when using the "-e" flag. >> >> Is there a silent return value of the incoming text that "-p" is then >> printing? >> > > Not a return value. > > From `ruby --help`: > -n assume 'while gets(); ... end' loop around your script > -p assume loop like -n but print line also like sed > > So it creates a loop around the (empty) expression; the loop keeps getting > the next line from stdin until EOF; and it prints the line. (Actually I > believe the line printed is the special variable $_, which gets writes to > implicitly. In an actual useful one-liner you would probably filter out > certain lines and/or run sub or gsub -- when invoked as Kernel methods, > these also implicitly write to $_.) > Btw, that's what other tools do as well: $ seq 1 5 | sed -e '' 1 2 3 4 5 Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --089e01493bf43a4a3f04df1b4d30 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Fri, Jun 14, 2013 at 1:14 AM, Eric Christopherson <echr= istopherson@gmail.com> wrote:
On Thu, Jun 13, 2013 at= 4:49 PM, Greg Ferguson <lists@ruby-forum.com> wrote:
How come Ruby outputs the incoming piped text when there i= s no code to
evaluate:

=A0 =A0 echo foo | ruby -pe ''

outputs: "foo"

Without "-p" there's no output, which I'd expect. Without= '' there's an
error, which I'd expect when using the "-e" flag.

Is there a silent return value of the incoming text that "-p" is = then
printing?

Not a return value.

From `ruby --help`:
=A0 -n =A0 =A0 =A0 =A0 = =A0 =A0 =A0assume 'while gets(); ... end' loop around your script
=A0 -p =A0 =A0 =A0 =A0 =A0 =A0 =A0assume loop like -n but print line a= lso like sed

So it creates a loop around the (= empty) expression; the loop keeps getting the next line from stdin until EO= F; and it prints the line. (Actually I believe the line printed is the spec= ial variable $_, which gets writes to implicitly. In an actual useful one-l= iner you would probably filter out certain lines and/or run sub or gsub -- = when invoked as Kernel methods, these =A0also implicitly write to $_.)

Btw, that's what other tools do as well:

$ seq 1 5 | sed -e ''
1
2
3
4
5

Kind regards

robert
<= div>
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.= com/
--089e01493bf43a4a3f04df1b4d30--