From: Pit Capitain Date: 2002-09-20T17:01:09+09:00 Subject: Re: Thoughts on improving usage of Regexp#match On 20 Sep 2002, at 9:47, Hal E. Fulton wrote: > Please feel free to point out obvious things > that I'm not seeing here. > > I was looking at a piece of code like this: > > sub1 = "" > if str =~ regex > sub1 = $1 > end > > and I thought three things: > 1. I've never liked the $1 -- I usually avoid > Perlish variables, even the common ones. > 2. I don't like the if. > 3. I don't like having to assign sub1 twice. > > So I tried writing: > > sub1 = regex.match(str)[1] || "" > > In theory this should be the same as the other > four lines of code. > > But it doesn't work because if the match > fails, #match returns nil and I can't call > [] on nil. > > (code with special MatchData object skipped) Hal, have you thought of introducing a new or enhancing the current Regex#match method with two additional optional parameters (the index and a default value)? Usage like > sub1 = regex.new_match(str, 1, "") (didn't find a good name) Regards, Pit