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> -- Posted via http://www.ruby-forum.com/.
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 On 7/28/06, christian <cmalpeli@gmail.com> wrote:> > 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> > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060728/8c8571e3/attachment.html
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 Prill 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-- Posted via http://www.ruby-forum.com/.
Maybe Matching Threads
- Removing a block of text within a string
- Question about ObjectSizeOffsetVisitor::visitGlobalVariable
- Question about ObjectSizeOffsetVisitor::visitGlobalVariable
- wrtiteBin in conjunction with seek : the position in the file is not good when writing
- a Question regarding glm for linear regression