From: Chad Perrin Date: 2007-04-17T04:03:10+09:00 Subject: Re: translate Perl diamond operator to Ruby On Mon, Apr 16, 2007 at 08:05:05PM +0900, Robert Klemme wrote: > On 16.04.2007 00:08, Chad Perrin wrote: > >On Mon, Apr 16, 2007 at 03:40:17AM +0900, Ken Bloom wrote: > >>On Mon, 16 Apr 2007 03:06:58 +0900, Chad Perrin wrote: > >> > >>>Over the years, I've found the following to be an excellent way to whip > >>>up quick, very useful Perl scripts: > >>> > >>> #!/usr/bin/perl > >>> > >>> while(<>) { > >>> # do stuff . . . > >>> } > >>> > >>>Is there an equivalent idiom in Ruby? So far as I'm aware, there's no > >>>catch-all diamond operator in Ruby that allows one to create a default > >>>input behavior for a script that accepts either a filename or piped > >>>output of another command the way this works in Perl. I certainly hope > >>>there's an equivalent, though. > >>ARGF.each do |line| > >> # do stuff > >>end > > > >Thanks muchly. Armed with this seed of knowledge (to mix a metaphor), I > >can find the rest via Google easily. I appreciate it. > > You can also use one of the command line switches -n or -p, e.g. > > 13:04:42 [tmp]: ls -l | ruby -ne 'puts "<#{$_[0,3]}>"' > > <-rw> > > 13:04:59 [tmp]: > > Kind regards > > robert That's useful, and very similar to Perl, but in practice I find that I don't take that approach very often at all. The only command/shebang line switch I use with any frequency in Perl is -l, for adding a newline to the end of all print statements, and since Ruby has puts I don't need to do that in Ruby. Usually, the programs I find myself writing don't neatly fit into a while loop -- except when writing a toy script to demonstrate something, in which case it usually pays to be explicit with the while loop anyway -- so the while (<>) { . . . } idiom in Perl (and now the ARGF.each do |foo| . . . end idiom in Ruby) is far more useful to me. -- CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ] unix virus: If you're using a unixlike OS, please forward this to 20 others and erase your system partition.