value of $1is retained

  • Thread starter Anders Vesterberg
  • Start date
A

Anders Vesterberg

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] =~ /\"?([\w\-: ]*)\"?/
# assigning $1 to something
end

Shouldn't the $-variables be reset after each regexp? Or is
it a better way to do this?

/Anders Vesterberg
http://www.vesterberg.se
 
K

Karl von Laudermann

Anders said:
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] =~ /\"?([\w\-: ]*)\"?/
# assigning $1 to something
end

Shouldn't the $-variables be reset after each regexp? Or is
it a better way to do this?


I know this doesn't answer your question as to why it works that way,
but you could instead write:

(0..key_colnames.length-1).each do |i|
if person_data[key_colnames] =~ /\"?([\w\-: ]*)\"?/
# assigning $1 to something
end
end

or even:

(0..key_colnames.length-1).each do |i|
myvar = (person_data[key_colnames] =~ /\"?([\w\-: ]*)\"?/) ?
$1 :
nil
end

Incidentally, what version of Ruby are you using? I was unable to
reproduce this behavior on Ruby 1.8.4 on Windows. This sample program:

"boom" =~ /(o)/
puts "$1 = #{$1.inspect}"

"boom" =~ /(a)/
puts "$1 = #{$1.inspect}"

"boom" =~ /(o)/
puts "$1 = #{$1.inspect}"

yields this output:

$1 = "o"
$1 = nil
$1 = "o"
 
D

dblack

Hi --

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] =~ /\"?([\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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top