From: Robert Klemme Date: 2011-06-07T03:49:31+09:00 Subject: Re: CORE - Altering Behaviour of "each do" (default param "item") On Mon, Jun 6, 2011 at 7:19 PM, Chad Perrin 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.  I 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).  For 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.  Specifically, I would expect these three commands to be > equivalent in effect: > >    seq 1 5 | perl -ne 'print' >    seq 1 5 | ruby -ne 'puts' >    seq 1 5 | ruby -ne 'p' > > For some reason, though, they are not.  The puts and p versions in Ruby > do not output anything at all (other than blank lines in the case of > puts).  If I use print in Ruby, though, the behavior is equivalent: > >    seq 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/