title: "RuntimeError: string modified" when I try gsub! incoming string inside each
body: <p>My method gets strings with some urls. Urls can have ascii only symbols or any another:</p>
  
  <pre><code>"тест non-latin url http://url.cоm and test only-latin url https://url.com"
  </code></pre>
  
  <p>I want to parse my incoming strings, and then change all matches in the original string as follows:</p>
  
  <pre><code>"тест non-url &lt;b&gt;[NON-LATIN LETTERS: http://url.cоm]&lt;/b&gt; and test only-latin url &lt;a href="https://url.com"&gt;https://url.com&lt;/a&gt;"
  </code></pre>
  
  <p>and pass it to the next method.</p>
  
  <p>I used <code>each</code> with <code>gsub!</code>:</p>
  
  <pre><code>def validate_url(string)
    validate_url = string.gsub(/((http|https):\/\/.*?)(\s|$|\n|\Z|\t|\r|&lt;.)/)
  
    validate_url.each do |link|
      if link.ascii_only?
        string.gsub!(link, "&lt;a href=\"#{link}\"&gt;#{link}&lt;/a&gt;")
      else
        string.gsub!(link, "&lt;b&gt;[NON-LATIN LETTERS: #{link}]&lt;/b&gt;")
      end
    end
  end
  </code></pre>
  
  <p>I see <code>RuntimeError: string modified</code> error and don't understand why and how to fix that.</p>