I have a string:
story = 'A long foo ago, in a foo bar baz, baz away...foobar'
I also have matches from this string (the dictionary is dynamic, it doesn't depend on me)
string_matches = ['foo', 'foo', 'bar', 'baz', 'baz', 'foobar'] # words can be repeated
How to replace each match with **foo**
? to get a result:
story = 'A long **foo** ago, in a **foo** **bar** **baz**, **baz** away...**foobar**'
for example my code:
string_matches.each do |word|
story.gsub!(/#{word}/, "**#{word}**")
end
returned:
"A long ****foo**** ago, in a ****foo**** **bar** ****baz****, ****baz**** away...****foo******bar**"