Forum: Ruby on Rails extending a class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
69d524565ee927f209b5dcc04f313450?d=identicon&s=25 Chris Sepic (csepic)
on 2006-02-25 19:30
I'm trying to add this to my project:

class String
   def escape_quotes
     self.gsub(/["']/) { |m| "\\#{m}" }
   end
end

I tried putting it in application_helper.rb, but it's not getting found.
Where does it need to go?

Thanks,
Chris
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-02-25 19:42
(Received via mailing list)
Hi --

On Sat, 25 Feb 2006, Chris Sepic wrote:

> I'm trying to add this to my project:
>
> class String
>   def escape_quotes
>     self.gsub(/["']/) { |m| "\\#{m}" }
>   end
> end
>
> I tried putting it in application_helper.rb, but it's not getting found.
> Where does it need to go?

Are you putting it inside the helper module, like this:

module MyHelper
   class String
   ...

If so, you're creating a new class called MyHelper::String.  You need
either to put the String stuff outside of any other module or class,
or do:

   class ::String

which means you want the top-level constant String.


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
This topic is locked and can not be replied to.