Extending a class


#1

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


#2

Hi –

On Sat, 25 Feb 2006, Chris S. 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 (removed_email_address@domain.invalid)
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