From: Dossy Date: 2002-09-20T09:56:30+09:00 Subject: Re: Thoughts on improving usage of Regexp#match On 2002.09.20, 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 Hee hee. > 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] || "" a = /x/.match("abc") && $~[1] || "" # a => "" b = /(b)/.match("abc") && $~[1] || "" # b => "b" c = /b/.match("abc") && $~[1] || "" # c => "" I think that #2 in your list above is a sign of Perl envy where Perl has no notion of nil vs empty string (which I brought up on this list a while back) to which Timmy Hammerquist said "even Larry Wall says you should test the success or failure of a regex!" Having to pre-initialize the variable with an empty string does seem kinda lame, but it's The Ruby Way, isn't it? -- 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)