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.

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

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://rails.aizatto.com/2007/06/01/ruby-and-open-classes/ (scroll down,
it’s an article)

hope it helps out :slight_smile:

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”

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!

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…

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 ’

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs