# Creating/modifying a class in Rails

**URL:** <https://www.ruby-forum.com/t/creating-modifying-a-class-in-rails/112651>\
**Category:** Rails\
**Created:** [August 21, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/creating-modifying-a-class-in-rails/112651 "2007-08-21T21:01:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@shane](https://www.ruby-forum.com/u/shane)\
**Post date:** [August 21, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/creating-modifying-a-class-in-rails/112651/1 "2007-08-21T21:01:41Z")

</div>

Forgive me if this is a really stupid question, I’ve only started  
learning Rails.

After some Googling and talking to some people, I still don’t know how  
to do this.

Basically, what I want to do is to be able to create a class that I  
can use anywhere in my application, kind of like the way you can use a  
method anywhere when you define it in the ApplicationHelper module.

Specifically, I don’t like using methods like truncate(string, length)  
and h(string), and I’d much rather use them in the form of  
string.truncate(length) and string.h(),

Could anybody tell me how to do this? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![shane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@shane](https://www.ruby-forum.com/u/shane)\
**Post date:** [August 21, 2007, 9:21pm UTC](https://www.ruby-forum.com/t/creating-modifying-a-class-in-rails/112651/2 "2007-08-21T21:21:43Z")

</div>

You should be able to created your class under the lib directory.

---

<div class="post-metadata">

**Author:** ![shane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@shane](https://www.ruby-forum.com/u/shane)\
**Post date:** [August 22, 2007, 9:29am UTC](https://www.ruby-forum.com/t/creating-modifying-a-class-in-rails/112651/3 "2007-08-22T09:29:43Z")

</div>

> Basically, what I want to do is to be able to create a class that I  
> can use anywhere in my application, kind of like the way you can use a  
> method anywhere when you define it in the ApplicationHelper module.

as of that:

> > You should be able to created your class under the lib directory.

> Specifically, I don’t like using methods like truncate(string, length)  
> and h(string), and I’d much rather use them in the form of  
> string.truncate(length) and string.h(),
> 
> Could anybody tell me how to do this? Thanks in advance.

if you want to define a method for an object (instance method), you’ll  
need to define that in that instance’s class; so, if you want to play  
around with strings, you’ll need to modify that class.

class String  
def truncate(size)  
self[0…size]  
# self is the instance being worked on  
end  
end

so now,

“apples”.truncate(3) # returns “app”

some more examples:

[http://www.donttrustthisguy.com/2005/12/31/ruby-extending-classes-and-method-chaining/](http://www.donttrustthisguy.com/2005/12/31/ruby-extending-classes-and-method-chaining/)  
[http://rails.aizatto.com/2007/06/01/ruby-and-open-classes/](http://rails.aizatto.com/2007/06/01/ruby-and-open-classes/) (scroll down,  
it’s an article)

hope it helps out 🙂

---

<div class="post-metadata">

**Author:** ![shane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@shane](https://www.ruby-forum.com/u/shane)\
**Post date:** [September 25, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/creating-modifying-a-class-in-rails/112651/4 "2007-09-25T23:05:29Z")

</div>

one way is to define them on the String class:

class String  
def h  
…do stuff…  
end  
end

save this in a file somewhere, e.g. lib/my\_string\_methods.rb

and include it in your environment.rb  
require File.dirname( **FILE** ) + “/…/lib/my\_string\_methods”

---

<div class="post-metadata">

**Author:** ![shane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@shane](https://www.ruby-forum.com/u/shane)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/creating-modifying-a-class-in-rails/112651/5 "2007-09-25T23:04:56Z")

</div>

Thanks a lot! This does exactly what I was looking for.

It’s annoying that I have restart script/server everytime I make a  
change, but it’ll do. Thanks!

---

<div class="post-metadata">

**Author:** ![shane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@shane](https://www.ruby-forum.com/u/shane)\
**Post date:** [November 28, 2007, 12:17pm UTC](https://www.ruby-forum.com/t/creating-modifying-a-class-in-rails/112651/6 "2007-11-28T12:17:56Z")

</div>

Shane wrote:

> Thanks a lot! This does exactly what I was looking for.
> 
> It’s annoying that I have restart script/server everytime I make a  
> change, but it’ll do. Thanks!

If you want to do modification without restarting the server instead of  
using

- require File.dirname( **FILE** ) + “/…/lib/my\_string\_methods” put it  
inside Rails::Initializer in your environment.rb using the  
config.load\_path directive.

config.load\_paths += %W( #{RAILS\_ROOT}/lib/)

cheers…

---

<div class="post-metadata">

**Author:** ![shane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@shane](https://www.ruby-forum.com/u/shane)\
**Post date:** [September 25, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/creating-modifying-a-class-in-rails/112651/7 "2007-09-25T23:10:10Z")

</div>

also keep in mind that rails defines some String helpers already.  
e.g. to truncate a string, you could do

‘this is my string’.first(5) # = 'this ’

or use core ruby methods:  
‘this is my string’[0,5] # = 'this ’
