Extending core classes?

I want to add some functionality to DateTime and stuff. I’m not sure
how I would go about extending it though…would I modify the class
itself in the core, extend it in another class, etc? Also, I don’t see
where I’d put it in my rails application…thanks!

Put it in the lib directory. It doesn’t matter what name you give it.

Ruby classes are cool in regards to being able to add functionality. In
other languages, doing:

class DateTime
#blah
end

when there’s already a DateTime class defined might throw an error!

Thankfully, Ruby understands that sometimes we want to add to something
so
doing

class DateTime
code
end

Will add that code to the DateTime class. Out of interest, what code are
you
adding to it?

On Dec 6, 2007 11:12 AM, Mike C [email protected] wrote:

I want to add some functionality to DateTime and stuff. I’m not sure
how I would go about extending it though…would I modify the class
itself in the core, extend it in another class, etc? Also, I don’t see
where I’d put it in my rails application…thanks!


Ryan B.

Interested to know the changes you like to make

Ryan’s right – Ruby classes are always open for extension. As for
where to put it in a Rails app… the ‘lib’ folder is probably the
most logical place if you’re working on code that is specific to your
application. If it’s something that you’d like to share with the
world (or possibly just some of your other projects) then build it
into a plugin.

Basically he was attempting to override the Rails method sum.

On Dec 7, 2007 4:24 PM, Ayyanar Aswathaman
[email protected]
wrote:

Interested to know the changes you like to make

Posted via http://www.ruby-forum.com/.


Ryan B.