From: Josh Cheek Date: 2011-06-07T02:35:39+09:00 Subject: Re: CORE - Altering Behaviour of "each do" (default param "item") --90e6ba53ac34f707ac04a50e8787 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Jun 6, 2011 at 12: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. > > -- > Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] > Also Perlish for regex within conditionals: $ echo hello world | ruby -W0 -ne 'puts $_ if "strings are objects"' hello world $ echo hello world | ruby -W0 -ne 'puts $_ if /regex are objects/' $ echo hello world | ruby -W0 -ne 'puts $_ if /i just came to say|hello/' hello world --90e6ba53ac34f707ac04a50e8787--