# Having trouble extending Date

**URL:** <https://www.ruby-forum.com/t/having-trouble-extending-date/140925>\
**Category:** Rails\
**Created:** [June 19, 2008, 9:16pm UTC](https://www.ruby-forum.com/t/having-trouble-extending-date/140925 "2008-06-19T21:16:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [June 19, 2008, 9:16pm UTC](https://www.ruby-forum.com/t/having-trouble-extending-date/140925/1 "2008-06-19T21:16:39Z")

</div>

Hi all,

I need to extend the Date class. Or more accurately, the  
ActiveSupport::CoreExtensions:📅:Calculations module. Everything  
works  
fine when I include the new method in that module itself. But when I  
try to  
move the extension out of that module and into my application space  
(e.g.,  
in the lib directory), I get a method not found error. I’m sure I’m  
missing  
something basic, but could sure use some help identifying what that  
thing  
is. I’ve tried various locations thinking that maybe I was just putting  
it  
in the wrong place, but nothing’s worked. Any help’s much appreciated!

Thanks,  
Bill

----------- calculations.rb module extension ---------------

module ActiveSupport #:nodoc:  
module CoreExtensions #:nodoc:  
module Date #:nodoc:  
# Enables the use of time calculations within Time itself  
module Calculations

```
     def adjust_start_for_weekends
        if self.strftime('%a') == 'Fri'
           return self.advance(:days => 2)
        elsif self.strftime('%a') == 'Sat'
           return self.advance(:days => 1)
        else
           return self
        end
     end

  end
end

```

end  
end

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [June 19, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/having-trouble-extending-date/140925/2 "2008-06-19T21:21:21Z")

</div>

Maybe require it in environment.rb?

require ‘calcualtions’

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [June 19, 2008, 9:39pm UTC](https://www.ruby-forum.com/t/having-trouble-extending-date/140925/3 "2008-06-19T21:39:03Z")

</div>

Alright… sorry for that first stupid answer.

You could write a special method and include it in  
ActiveSupport::CoreExtensions:📅:Calculations but it probably  
won’t do you any good. Without digging around too much it appears the  
methods in ActiveSupport::CoreExtensions:📅:Calculations are being  
included into the Date class. Adding your special method to  
ActiveSupport::CoreExtensions:📅:Calculations probably won’t get  
it included in the Date class.

The solution? Include your method directly into the Date class.

#my\_calculations.rb  
module MyCalculations  
def adjust\_start\_for\_weekends  
if self.strftime(’%a’) == ‘Fri’  
return self.advance(:days =\> 2)  
elsif self.strftime(’%a’) == ‘Sat’  
return self.advance(:days =\> 1)  
else  
return self  
end  
end  
end

Date.send :include, MyCalculations

and then require it in environment.rb

require ‘my\_calculations’

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [June 19, 2008, 11:28pm UTC](https://www.ruby-forum.com/t/having-trouble-extending-date/140925/4 "2008-06-19T23:28:16Z")

</div>

Hi Julian,

julian wrote:

> Alright… sorry for that first stupid answer.

Not stupid at all. It got me thinking. As soon as I put a ‘require’ in  
application.rb, everything works as expected. Despite having ‘said out  
loud’ that I needed to extend the calculations module, my brain seems to  
have held onto the notion that I was exending the Date class. Rails  
picks  
up Class extentions automagically. We have to tell it to pick up  
modules.  
Duh ;-p

Thanks for helping me clear the ‘brain fart’ 😉

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [June 20, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/having-trouble-extending-date/140925/5 "2008-06-20T14:26:44Z")

</div>

Hi Matthew,

Matthew R. Jacobs wrote:

> just a quick question;  
> isn’t “date.wday == 5” a cleaner way of saying is it friday?

Depends on what you mean by ‘cleaner’ I guess. I try to write code  
that’s  
understandable to anybody, not just folks who know Ruby well enough, in  
this  
case, to know what day of the week its calendar starts on. As far as  
that  
goes, I don’t trust my own memory well enough to use the above without  
at  
least testing it with strftime to make sure wday == 5 is Friday 😉

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [June 20, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/having-trouble-extending-date/140925/6 "2008-06-20T14:46:54Z")

</div>

On 20 Jun 2008, at 13:26, Bill W. wrote:

> Hi Matthew,
> 
> Matthew R. Jacobs wrote:
> 
> > just a quick question;  
> > isn’t “date.wday == 5” a cleaner way of saying is it friday?
> 
> Depends on what you mean by ‘cleaner’ I guess. I try to write code  
> that’s

Until you run on a localised system where strftime returns Freitag or  
something 🙂

Fred

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [June 20, 2008, 3:19pm UTC](https://www.ruby-forum.com/t/having-trouble-extending-date/140925/7 "2008-06-20T15:19:34Z")

</div>

Hi Fred,

Frederick C. wrote:

> Until you run on a localised system where strftime returns  
> Freitag or something 🙂

Very good point. The nature of “the simplest thing that could possibly  
work” is context-sensitive 😉

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [June 20, 2008, 1:40am UTC](https://www.ruby-forum.com/t/having-trouble-extending-date/140925/8 "2008-06-20T01:40:47Z")

</div>

Bill W. wrote:

> ----------- calculations.rb module extension ---------------
> 
> module ActiveSupport #:nodoc:  
> module CoreExtensions #:nodoc:  
> module Date #:nodoc:  
> # Enables the use of time calculations within Time itself  
> module Calculations
> 
> ```
> def adjust_start_for_weekends
> if self.strftime('%a') == 'Fri'
> return self.advance(:days => 2)
> elsif self.strftime('%a') == 'Sat'
> return self.advance(:days => 1)
> else
> return self
> end
> end
> 
> end
> end
> 
> ```
> 
> end  
> end

just a quick question;  
isn’t “date.wday == 5” a cleaner way of saying is it friday?

don’t really like strftime.

although  
Date::DAYNAMES[day.wday] == “Friday”

is probably worse
