From: dblack@... Date: 2006-08-08T22:36:54+09:00 Subject: Re: value of $1is retained Hi -- On Tue, 8 Aug 2006, Anders Vesterberg wrote: > Hi > I am using regexp in a loop to extract a value from a > string. I have noticed that when string is nil $1 keeps its > old value, which means that the expression after regexp > does not get the correct value (which should be nil). The > code looks like this > > (0..key_colnames.length-1).each do |i| > person_data[key_colnames[i]] =~ /\"?([\w\-: ]*)\"?/ > # assigning $1 to something > end > > Shouldn't the $-variables be reset after each regexp? Or is > it a better way to do this? If you're calling =~ on nil, no match operation takes place. Object#=~ returns false, and NilClass doesn't override it. David -- http://www.rubypowerandlight.com => Ruby/Rails training & consultancy ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <----- http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log http://www.manning.com/black => book, Ruby for Rails http://www.rubycentral.org => Ruby Central, Inc.