I would like to loop through regex matches and replace each match individually in the loop.
For example:
content.scan(/myregex/).each do |m|
m = 'new str'
end
How could I do that?
The reason why I want to do that is because each match will be replaced with a different output from a function.
Thanks for help
The following form of the gsub
method will do exactly what you want:
gsub(pattern) {|match| block } → new_str
See http://ruby-doc.org/core-2.0.0/String.html#method-i-gsub for documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With