# Gsub wrapper re-submitted

**URL:** <https://www.ruby-forum.com/t/gsub-wrapper-re-submitted/76033>\
**Category:** Ruby\
**Created:** [March 3, 2006, 12:15am UTC](https://www.ruby-forum.com/t/gsub-wrapper-re-submitted/76033 "2006-03-03T00:15:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![hector](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/43a26b/32.png) [@hector](https://www.ruby-forum.com/u/hector)\
**Post date:** [March 3, 2006, 12:15am UTC](https://www.ruby-forum.com/t/gsub-wrapper-re-submitted/76033/1 "2006-03-03T00:15:07Z")

</div>

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
