# Rails related Ruby question

**URL:** <https://www.ruby-forum.com/t/rails-related-ruby-question/161677>\
**Category:** Rails\
**Created:** [February 28, 2009, 9:51pm UTC](https://www.ruby-forum.com/t/rails-related-ruby-question/161677 "2009-02-28T21:51:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [February 28, 2009, 9:51pm UTC](https://www.ruby-forum.com/t/rails-related-ruby-question/161677/1 "2009-02-28T21:51:56Z")

</div>

I’m doing an app that has a very frequent need to compare dates. My  
code is littered with things like:

return if foo.date \< 10.days.ago

Each and every time I do one of these I have to stop and think it  
through to get the sense of the comparison right. “Let’s see, recent  
dates are bigger than older dates, so if I want this to happen when the  
date on the left is longer ago than the date on the right…” You get  
the idea.

I want to open up the date class and add a couple of methods to it like  
“is\_more\_recent\_than” and “is\_longer\_ago\_than”, so I don’t have to think  
about it anymore and so my code is more readable.

There are two things I’m not sure of with this, when I define the  
method, what does the method def syntax look like? Maybe:

def is\_more\_recent\_than(right\_hand\_value)  
self \> right\_hand\_value # just had to think about this for the last  
time!  
end

and the other thing is, where can I put this such that it will be  
available from controllers, and from models and from views? do I have  
to put it in more than one place to have it be automatically available  
(without doing an include when I want to use it) everywhere? Maybe it  
needs to be in one of my Rails files instead of in my app project?

thanks,  
jp

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [February 28, 2009, 11:06pm UTC](https://www.ruby-forum.com/t/rails-related-ruby-question/161677/2 "2009-02-28T23:06:13Z")

</div>

A simple and clean way (and also the preffered way of doing this) is  
by a Rails plugin.

Create a folder under vendor/plugins, maybe “date\_extensions”, create  
an “init.rb” file. Your init.rb would look like this:

Date.class\_eval do

def is\_older\_than( other\_date )  
self \< other\_date  
end

end

And so on, adding the other methods you want. For more about ruby  
method definition, read the metaprogramming chapter from →   
[http://oreilly.com/catalog/9780596516178/toc.html](http://oreilly.com/catalog/9780596516178/toc.html)

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

On Sat, Feb 28, 2009 at 5:51 PM, Jeff P.

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [March 1, 2009, 12:05am UTC](https://www.ruby-forum.com/t/rails-related-ruby-question/161677/3 "2009-03-01T00:05:16Z")

</div>

MaurÃ­cio Linhares wrote:

> A simple and clean way (and also the preffered way of doing this) is  
> by a Rails plugin.
> 
> Create a folder under vendor/plugins, maybe “date\_extensions”, create  
> …  
> Maurï¿½cio Linhares  
> [http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
> (en)

Thanks Mauricio,  
I had no idea it was that simple to make a plugin!

thanks,  
jp
