gsub wrapper re-submitted

H

Hector

Help!

How do I get $1, $2, and $3 to get interpolated in the following
snippet?

string = 'This is a test of the emergency'
regexp = Regexp.new('(\W)+(\w\w)(\W)+')
replacement = '$1->$2<-$3'
user_wants_upcase=FALSE

# Note: All variables above are set by the user of the program, so I
don't know
# what they will contain before runtime.

string.gsub!(regexp) {
return_value = replacement # Here, how do I get $1, $2, and $3 to
interpolate?

if (user_wants_uppercase)
return_value.upcase! || return_value
end

return_value
}

I want return_value to be -> 'This ->is<- a test ->of<- the emergency'

Note: I know that I can get it to work with the non-block version of
gsub! but I need to use the block version.

Thanks

Hector
 

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

Similar Threads


Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top