Forum: Ruby gsub wrapper re-submitted

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9fea6e7f0cb9a8b8affd5f870458d982?d=identicon&s=25 Hector (Guest)
on 2006-03-03 00:15
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
This topic is locked and can not be replied to.