From: ChrisO Date: 2004-09-16T09:14:51+09:00 Subject: Re: Ex-Perl coders: Howz it feel to convert to Ruby? Bill Kelly wrote: > Hi, > > From: "Chris" > >>As already stated, I can usually whack out a concise, tight solution in >>Perl in just a little bit of time. I'm anxious to see if I can get to >>that place in Ruby and what that would look like. This sounds like Ruby >>might be more suited to "bigger" tasks and Perl to the small ones. I'm >>sure others will jump in here momentarily and dispute this... > > > One of the things that thrilled me with ruby was that > it didn't force me to give up quick one-off hack style > scripting, in order to have a cleaner language for bigger > tasks.... > > I invoke ruby from the command line fairly frequently, > for random things like: > > ruby -i -pe 'gsub(/\015/,"")' *.cpp *.h > > If I recall my perl correctly (been about three years now) > the perl is slightly shorter: > > perl -i -pe 's/\015//g' *.cpp *.h > > But ruby has proved sufficiently concise for my needs > in this regard. Actually, sometimes the ruby comes out > shorter than what I think the equivalent perl would be. > > Couple days ago a friend who has been learning ruby > asked me about making a program to gather and print a > sorted list of all unique IP addresses in a logfile he > had. (He said a simple string-based sort was OK, and > the logfile structure was simple enough that a very > basic regexp would identify IP addrs without false > positives.) I first approached the problem with a > hash, as I would have in Perl... and then I remembered > ruby has Array#uniq.... So the solution ended up > being: > > puts ARGF.read.scan(/\d+\.\d+\.\d+\.\d+/).uniq.sort > This is where I'm looking to get with Ruby and I still reach for Perl because I don't know Ruby as well. It happened today at work. I needed to extract some server names from a piece of Perl code (text) that looked like this: { SERVER => "servername1", SHARE = $Default }, { SERVER => "servername2", SHARE = $Default }, { SERVER => "servername3", SHARE = $Default }, { SERVER => "servername4", SHARE = $Default }, { SERVER => "servername5", SHARE = $Default }, { SERVER => "servername6", SHARE = $Default }, { SERVER => "servername7", SHARE = $Default }, { SERVER => "servername8", SHARE = $Default }, (The server names were more unique than the contrived names above.) In Perl it was: perl -e 'while (<>) { /"(\w*)"/; print "$1\n" }' file-snippet.txt In Ruby? I didn't have the time to figure it out. Which is the agonizing point. Later, I did the following in Ruby to satisfactory effect: ruby -e 'ARGF.each { |line| line.scan( /"(\w*)"/ ) { |frag| puts frag } }' file-snippet.txt Perhaps someone can refine this while we're at it. It's hard not to notice that the Perl was more concise. Perhaps my ignorance can be made right...? Can this be shortened in Ruby? Surely... -ceo