# Newbie Question -- Override String functionality

**URL:** <https://www.ruby-forum.com/t/newbie-question-override-string-functionality/102122>\
**Category:** Rails\
**Created:** [August 11, 2006, 2:16am UTC](https://www.ruby-forum.com/t/newbie-question-override-string-functionality/102122 "2006-08-11T02:16:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cdc98d/32.png) [@cman](https://www.ruby-forum.com/u/cman)\
**Post date:** [August 11, 2006, 2:16am UTC](https://www.ruby-forum.com/t/newbie-question-override-string-functionality/102122/1 "2006-08-11T02:16:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cdc98d/32.png) [@cman](https://www.ruby-forum.com/u/cman)\
**Post date:** [August 11, 2006, 3:26am UTC](https://www.ruby-forum.com/t/newbie-question-override-string-functionality/102122/2 "2006-08-11T03:26:43Z")

</div>

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/](http://blog.hasmanythrough.com/)

---

<div class="post-metadata">

**Author:** ![cman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cdc98d/32.png) [@cman](https://www.ruby-forum.com/u/cman)\
**Post date:** [August 11, 2006, 5:48am UTC](https://www.ruby-forum.com/t/newbie-question-override-string-functionality/102122/3 "2006-08-11T05:48:11Z")

</div>

> > 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”

---

<div class="post-metadata">

**Author:** ![cman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cdc98d/32.png) [@cman](https://www.ruby-forum.com/u/cman)\
**Post date:** [August 11, 2006, 10:43pm UTC](https://www.ruby-forum.com/t/newbie-question-override-string-functionality/102122/4 "2006-08-11T22:43:48Z")

</div>

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/](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

---

<div class="post-metadata">

**Author:** ![cman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cdc98d/32.png) [@cman](https://www.ruby-forum.com/u/cman)\
**Post date:** [August 15, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/newbie-question-override-string-functionality/102122/5 "2006-08-15T14:16:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cdc98d/32.png) [@cman](https://www.ruby-forum.com/u/cman)\
**Post date:** [August 11, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/newbie-question-override-string-functionality/102122/6 "2006-08-11T23:35:45Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![cman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cdc98d/32.png) [@cman](https://www.ruby-forum.com/u/cman)\
**Post date:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/newbie-question-override-string-functionality/102122/7 "2006-08-15T14:19:43Z")

</div>

> From an organisational perspective, I would think this would belong in  
> app/helper/application\_helper.rb or perhaps an engine.
