On 2002.05.19, Tim Hammerquist <tim / vegeta.ath.cx> wrote: > [ stealth cc's considered harmful ] [ accidental use of Mail-Followup-To considered harmful ] Date: Sun, 19 May 2002 09:27:47 +0900 From: Tim Hammerquist <tim / vegeta.ath.cx> Newsgroups: comp.lang.ruby Subject: Re: Ruby regex question To: ruby-talk / ruby-lang.org (ruby-talk ML) Reply-To: ruby-talk / ruby-lang.org Mail-Followup-To: timmy / cpan.org I'm mailing a follow-up to a mailing list, not replying to your mail. The difference between "r" and "L" in mutt... ;-) > As I said, I don't know what codebase you'll be using this in, but you > seem very certain that the string will _always_ match the regex. This > is just not an assumption real experiences have let me make safely. > Just a small difference in either part of the match and you'd continue > using the $x and $y vars even though they're undefined (Perl) or nil > (Ruby). Right, they're nil. Later on, when it matters, I can (and should) test to make sure x.nil? and y.nil? are false. Is it typical to test values as they're set (when you execute the regex) or right before you use them? What's the more defensive way of coding? > > From: Randal L. Schwartz (merlyn / stonehenge.com) [...] > > Otherwise, on a failed match, the $1 you're printing is the *previous* > > match's $1. Is this true of Ruby as well? "foo bar" =~ /(foo bar)/ # => 0 x = $1 # => "foo bar" "fooxbar" =~ /(foo bar)/ # => nil y = $1 # => nil (not "foo bar" like Perl) This is good news. > Yes, he's talking about Perl. However, Ruby's behavior mimics Perl's, > at least insofar as "nothing changes if nothing matches". This isn't true, at least not in Ruby 1.6.7 which is what I tested with. Did this get "broken" in Ruby 1.7.x? > But oh well. Someone who's been programming and hacking for over 30 > years and probably fixed more bugs than I've had the chance to _make_ > probably doesn't even know what he's talking about... Ne? I certainly respect Randal's abilities and the suggestion that it's smart to test regex success or failure in _Perl_ is very wise, that's because Perl's regex implementation is busted with regard to setting $1..$n to undef on failed regex. That "feature" is by now a well known bug and it forces Perl programmers to "work around it" by testing regex success rather than simply encourage programmers to test for valid inputs and produce valid outputs. I'd rather not have to test for regex success before assigning matches to variables if I didn't have to. I'd rather just code my methods to test for valid inputs -- I have to do this anyway if I'm writing code that isn't going to blow up on bad inputs. Why should I _have_ to do more than just due diligence unless the _language_ forces me to. I'm moving to Ruby because I'm looking for something that isn't quite as broken as Perl but offers me a better OO implementation than [incr Tcl]. Not because I "can write Perl in any language!" -- Dossy -- Dossy Shiobara mail: dossy / panoptic.com Panoptic Computer Network web: http://www.panoptic.com/ "He realized the fastest way to change is to laugh at your own folly -- then you can let go and quickly move on." (p. 70)