Newbie Question -- Override String functionality

Hello,

I would like to add functionality to the String class…where do I do
this?

Do I create a new model (if so, what do I call it?) and put something
like:

class String

def my_new_string_method
   blah blah
end

end

Thanks for any pointers.

cman wrote:

Hello,

I would like to add functionality to the String class…where do I do
this?

Do I create a new model (if so, what do I call it?) and put something like:

class String
def my_new_string_method
blah blah
end
end

You don’t need to create a new model. You can just paste that code to
extend String into your config/environments.rb and you’ll be good to go.
That’s a bit messy way to organize monkey patches, but it works. If you
get to the point where that becomes a pain, move the monkey patches into
files in the lib directory and require them from environment.rb.


Josh S.
http://blog.hasmanythrough.com/

class String
def my_new_string_method
blah blah
end
end

You don’t need to create a new model. You can just paste that code to
extend String into your config/environments.rb and you’ll be good to go.
That’s a bit messy way to organize monkey patches, but it works. If you
get to the point where that becomes a pain, move the monkey patches into
files in the lib directory and require them from environment.rb.

Another option is to bundle your extension methods into a module, and
include it into the String class.

Say:
module MyStringModule
def my_foo
“foo.#{to_s}”
end
end

String.send(:include, MyStringModule)

Note the use of send, as the include method is private.

The nifty part of this implementation is that you get some information
in the class about which modules it uses.

Type:
String.ancestors

and the list will include “MyStringModule”

Josh S. wrote:

cman wrote:

Hello,

I would like to add functionality to the String class…where do I do
this?

Do I create a new model (if so, what do I call it?) and put something like:

class String
def my_new_string_method
blah blah
end
end

You don’t need to create a new model. You can just paste that code to
extend String into your config/environments.rb and you’ll be good to go.
That’s a bit messy way to organize monkey patches, but it works. If you
get to the point where that becomes a pain, move the monkey patches into
files in the lib directory and require them from environment.rb.


Josh S.
http://blog.hasmanythrough.com/

I tried adding it to configs/environments.rb, and i get the following
error when booting WebBrick: Invalid char ‘\223’ in expression, Invalid
char ‘\224’ in expression.

Here is the code:
class String
def capitalize_each
self.split(â? â??).each{|word| word.capitalize!}.join(â? â??)
end
def capitalize_each!
replace capitalize_each
end
end

On Aug 10, 2006, at 8:16 PM, cman wrote:

def my_new_string_method
blah blah
end

end

Thanks for any pointers.

I usually put this sort of stuff in /lib and then require it from
config/environment.rb
-Mat

I tried adding it to configs/environments.rb, and i get the following
error when booting WebBrick: Invalid char ‘\223’ in expression, Invalid
char ‘\224’ in expression.

Here is the code:
class String
def capitalize_each
self.split(â? â??).each{|word| word.capitalize!}.join(â? â??)
end
def capitalize_each!
replace capitalize_each
end
end

Not for certain on this, a likely cause is fancy quotes. If you copy
and pasted this code from somewhere (like a blog), the double-quotes may
be fancy quotes. Just try retyping the quotes in your text editor.

From an organisational perspective, I would think this would belong in
app/helper/application_helper.rb or perhaps an engine.