# String replacement?

**URL:** <https://www.ruby-forum.com/t/string-replacement/100281>\
**Category:** Rails\
**Created:** [July 28, 2006, 5:04am UTC](https://www.ruby-forum.com/t/string-replacement/100281 "2006-07-28T05:04:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![christian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@christian](https://www.ruby-forum.com/u/christian)\
**Post date:** [July 28, 2006, 5:04am UTC](https://www.ruby-forum.com/t/string-replacement/100281/1 "2006-07-28T05:04:41Z")

</div>

Hello,

What is the best method to replace a substring within a string?

I’m trying this:  
url.gsub(string\_to\_replace,replacement)

but i get the following error:  
undefined method `rewrite’ for #String:0x3711a58

---

<div class="post-metadata">

**Author:** ![christian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@christian](https://www.ruby-forum.com/u/christian)\
**Post date:** [July 28, 2006, 8:32am UTC](https://www.ruby-forum.com/t/string-replacement/100281/2 "2006-07-28T08:32:09Z")

</div>

Hi Christian,

what you are doing should work. I don’t find anything on this error  
message  
by phrase searching your error message (without the =x37… part of  
course)  
on google but you’ll find something on the rails mailing list archive.

Seems as if the name ‘url’ is kind of a reserved word in rails. Please  
try  
(just for testing purposes) to simply rename your String ‘url’ or clone  
your  
column ‘url’ to another string (not named ‘url’) before you perform your  
replacement.

By the way: On gsub you might want to use a regex on the  
string\_to\_replace  
part:

my\_string.gsub(/my\_regex/, ‘replace\_it’)

Cheers,  
Jan

---

<div class="post-metadata">

**Author:** ![christian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@christian](https://www.ruby-forum.com/u/christian)\
**Post date:** [July 28, 2006, 2:24pm UTC](https://www.ruby-forum.com/t/string-replacement/100281/3 "2006-07-28T14:24:16Z")

</div>

Hi Jan,

What you suggested worked! It seems url is a reserved word. I switched  
the variable name to my\_url, and the replace ran properly.

Thank you for your suggestion

Jan P. wrote:

> Hi Christian,
> 
> what you are doing should work. I don’t find anything on this error  
> message  
> by phrase searching your error message (without the =x37… part of  
> course)  
> on google but you’ll find something on the rails mailing list archive.
> 
> Seems as if the name ‘url’ is kind of a reserved word in rails. Please  
> try  
> (just for testing purposes) to simply rename your String ‘url’ or clone  
> your  
> column ‘url’ to another string (not named ‘url’) before you perform your  
> replacement.
> 
> By the way: On gsub you might want to use a regex on the  
> string\_to\_replace  
> part:
> 
> my\_string.gsub(/my\_regex/, ‘replace\_it’)
> 
> Cheers,  
> Jan
