From: Chris Olive Date: 2001-10-23T08:50:24+09:00 Subject: [ruby-talk:23006] Re: Newbie: Ruby equiv for Perl while (<>)??? Thanks all. I discovered the -n switch shortly after submitting my question to Usenet. (That's always the quickest way for me to find anything: submit the question, and then I find it immediately thereafter...) The ARGF object is nice too. I'll have to use that too. Been writing Ruby pretty much everyday, when I can. It totally rocks... I just need more exposure to all the classes and methods available; that's the only hangup. I'm converting to Ruby; sorry Perl. (Though I will probably still use Perl alot since the rest of the world still doesn't seem to know about Ruby -- a check on Monster.Com for Ruby jobs turned up one company that mentioned it. One.) I have yet to see whether there is MySQL support for this... That would be a big issue for me too... chris ----- Chris Olive colive@NOSPAM.technologEase.com "David Alan Black" wrote in message news:Pine.LNX.4.30.0110210742510.4940-100000@candle.superlink.net... > Hello -- > > On Sun, 21 Oct 2001, Michael Neumann wrote: > > > David Alan Black wrote: > > > > > > > $ perl -e 'while (<>) { if (/^\d/) { print "Starts with digit\n" } else > > > > { print "Nope\n" } }' > > > > > > You could do: > > > > > > $ ruby -we 'while gets; puts(/^\d/ && "Starts with digit" || "Nope"); end' > > > > > > or if you want to give your fingers more exercise (and see a couple of > > > other little Ruby things in action): > > > > > > $ ruby -we 'while gets; puts begin if /^\d/ then "Starts with digit" \ > > > else "Nope" end end end' > > > > > > > Using the -n (or -p) option, you can ommit the while-loop: > > > > $ ruby -ne 'puts /^\d/ ? "Starts with digit" : "Nope"' > > > > With -p option (prints out $_ in each iteration): > > > > $ ruby -pe '$_ = /^\d/ ? "Starts with digit" : "Nope"' > > Interesting! At first I thought the = was supposed to be =~, but now > I get it :-) > > The only thing is that you'd need an 'l' switch in there too (or stick > \n's on the strings).) > > > David > > -- > David Alan Black > home: dblack@candle.superlink.net > work: blackdav@shu.edu > Web: http://pirate.shu.edu/~blackdav > > >