String replacement?

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

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

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