# Strange 'string modified' error!

**URL:** <https://www.ruby-forum.com/t/strange-string-modified-error/90543>\
**Category:** Rails\
**Created:** [March 29, 2007, 8:45pm UTC](https://www.ruby-forum.com/t/strange-string-modified-error/90543 "2007-03-29T20:45:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [March 29, 2007, 8:45pm UTC](https://www.ruby-forum.com/t/strange-string-modified-error/90543/1 "2007-03-29T20:45:11Z")

</div>

Hi,

Got this strange error message coming from the second instance of the  
‘scan’ method (line 39) on the text string. Any clues. Really stumped  
me… It was working fine, then suddenly - boom it stopped.

```
...

text.scan(/\/\.asset\.[0-9]*\.url\./) do |url|
  id = url.gsub(/[^0-9]/,'').to_i
  if asset = Asset.find(id)
    text.gsub!(url, '/' + asset.url_for_link)
  end
end

```

39 text.scan(/.asset.[0-9]\*.title./) do |url|  
id = url.gsub(/[^0-9]/,’’).to\_i  
if asset = Asset.find(id)  
text.gsub!(url, CGI.escapeHTML(asset.title))  
end  
end

```
...

```

The error is (in summary):

–

RuntimeError in SiteController#show\_page

string modified

RAILS\_ROOT: /var/www/downing/current/config/…

Application Trace  
…  
#{RAILS\_ROOT}/app/filters/xhtml/xhtml\_filter.rb:39:in `scan’  
…

–

Many thanks in advance.

Best,

~ Mark D.

---

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [March 29, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/strange-string-modified-error/90543/2 "2007-03-29T23:07:59Z")

</div>

In the end I fixed it. I initially replaced the ‘scan’ with ‘gsub!’ as  
the ‘text’ string was being changed inside the block passed to ‘scan’  
which was causing the ‘string modified’ error.

However, even with ‘gsub!’ I was receving the same error. This seems to  
be becuase the multiple ‘gsub!’ invocations are not thread safe? In the  
end I had to use a mutex:

require ‘thread’  
mutex = Mutex.new

…

mutex.synchronize do  
text.gsub!(//.asset.([0-9]\*).url./) { ‘/’ +  
Asset.find($1).url\_for\_link }  
end

mutex.synchronize do  
text.gsub!(/.asset.([0-9]\*).title./) {  
CGI.escapeHTML(Asset.find($1).title) }  
end

…

Not sure if this is actually the best solution, but it does work now!

~ Mark D.
