Is there a gsub alternative for ruby ​​that can run a method every time a match is made?

I need to run a script that will rewrite the path to the folder of the html file, there will be many matches and the replacement string needs to be replaced, for example

"Html string".gsub( /images/([a-zA-Z0-9\-]+)/, "/images/#{replacement_method($1)}/" )

      

the problem is gsub only, at least as far as I know, will only be triggered by the replacement_method () method once, and I need it to run every time since the replacement string changes appeared in the folder row.

Is there a way to make this work with gsub? something like replace function in wordpress? Any other realistic approaches?

+3


source to share


1 answer


You have to use a block:

"Html string".gsub( /images/(folder)/) { |match| "/images/#{replacement_method(match)}/" }

      



The block will be called for every match in the string.

+11


source







All Articles