From: David Alan Black Date: 2002-05-19T21:50:38+09:00 Subject: Re: Ruby regex question Hi -- On Sun, 19 May 2002, Dossy wrote: > On 2002.05.19, Tim Hammerquist wrote: > > > > Most Perl code from gurus checks the match success immediately. > > > > Most Ruby idioms do the same. > > Really? > > -r--r--r-- 1 root root 217854 Oct 5 2001 /usr/lib/perl5/5.6.1/CPAN.pm > > 3086 # read header > 3087 my($line_count,$last_updated); > 3088 while (@lines) { > 3089 my $shift = shift(@lines); > 3090 last if $shift =~ /^\s*$/; > 3091 $shift =~ /^Line-Count:\s+(\d+)/ and $line_count = $1; > 3092 $shift =~ /^Last-Updated:\s+(.+)/ and $last_updated = $1; [...] > > Maybe this portion of CPAN.pm wasn't written by a Perl guru. But in this example, the matches are indeed being checked, prior to saving $1. > You are right though, that it is more common to find regex > success immediately with the regex evaluation. Perhaps > it's because with Perl, you can't know if $1 is valid (applies > to the regex you just evaluated) _unless_ you test if the > regex succeeded, which _forces_ you to test for the regex > success. Ruby doesn't force you to do this, so the only > reason for it to be a Ruby idiom is because it's a carry-over > from Perl. I agree. Of course there's nothing wrong with checking for the match, and there are situations where it's precisely what one wants to do. But I agree that Perl wisdom doesn't translate directly into Ruby here, since the two languages handle this so differently. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav