Creating/modifying a class in Rails

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.

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"

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 '

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

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!