see gsub doc:
str.gsub (pattern) {| match | block} → new_str
In the block form, the current matching string is passed as a parameter, and variables such as $ 1, $ 2, $ `, $ &, and $ will be set accordingly. The value returned by the block will be replaced with a match for each call.
"a lower upper b".gsub(/(lower) (upper)/){|s| $1 + " " + $2.upcase}
source
share