On Mon, Jun 6, 2011 at 7:19 PM, Chad Perrin <code / apotheon.net> wrote:
> On Tue, Jun 07, 2011 at 01:40:25AM +0900, Robert Klemme wrote:
>>
>> $ seq 1 5 | ruby19 -ne 'p $_'
>> "1\n"
>> "2\n"
>> "3\n"
>> "4\n"
>> "5\n"
>
> Holy crap. =A0I had no idea.
>
> I don't think the existence of $_ is really a completely egregious
> problem (though I do dislike its ill fit in Ruby). =A0For a moment there =
I
> thought it was an incomplete implementation of $_, but I realized that
> for some reason "puts" and "p" do not work as I would expect in this
> context. =A0Specifically, I would expect these three commands to be
> equivalent in effect:
>
> =A0 =A0seq 1 5 | perl -ne 'print'
> =A0 =A0seq 1 5 | ruby -ne 'puts'
> =A0 =A0seq 1 5 | ruby -ne 'p'
>
> For some reason, though, they are not. =A0The puts and p versions in Ruby
> do not output anything at all (other than blank lines in the case of
> puts). =A0If I use print in Ruby, though, the behavior is equivalent:
>
> =A0 =A0seq 1 5 | ruby -ne 'print'
>
> I suspect I missed something that should be obvious but I have no idea
> why puts and p do not seem to be aware of $_ while print does behave as
> though aware of it in Ruby.

There's also -p:

Robert@babelfish ~
$ seq 1 5 | ruby19 -pe ''
1
2
3
4
5

Robert@babelfish ~
$ seq 1 5 | ruby19 -pe 'gsub /^/, "X"'
X1
X2
X3
X4
X5

Robert@babelfish ~
$

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/