Skip to content

Instantly share code, notes, and snippets.

@alterisian
Created June 5, 2011 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alterisian/1009253 to your computer and use it in GitHub Desktop.
Save alterisian/1009253 to your computer and use it in GitHub Desktop.
Weekends left til the end of summer
#Author: Ian Moss aka oceanician : http://twitter.com/oceanician
#First Published: https://gist.github.com/1009253
#I'm running as part of a rails project with:
# ruby script/runner lib\weekends.rb
# Returns parameter from_date if it is a Friday.
def next_friday( from_date )
while from_date.cwday!=5
from_date = from_date + 1.day
end
from_date
end
def weekends_til( end_date )
weekends = []
friday_count = next_friday( Date.today )
while( friday_count <= end_date )
weekends << friday_count
friday_count = friday_count + 7.days
end
weekends
end
#calculate all the weekends from now, until the middle of September.
#I'm running as part of a rails project with:
# ruby script/runner lib\weekends.rb
#future = Date.today + 5.months + 3.weeks
# Notionally decided 21st September is the end of Summer!
future = Date.civil(y=2011, m=9, d=21)
weekends=weekends_til(future)
puts "Make the most of the next #{weekends.size} weekends until, "+future.to_s(:long)
weekends.each{|w| puts "From: Fri "+w.to_s+" to Sun " +(w+3.days).to_s}
@alterisian
Copy link
Author

Nothing special - and if you spot an optimisation, please do tell me it.
Just thought it might be useful for those calculating the weekends left in Summer.

@p7r
Copy link

p7r commented Jun 6, 2011

Question: Where's your test coverage? :-) Took me a couple of minutes to get what you were trying to do here, test would help... :-P

So, to my eye, the variables are badly named (friday_count holds Date objects of upcoming Fridays, not a count of the number of Fridays which the name suggests - and of course in Ruby we always use arrays/hashes and then .size or ranges with .to_a.size to get a count so initially I thought you were being a C programmer who had just picked up Ruby), and I think it can be made to look/feel a little more elegant.

For example, I'd not do this as a standalone procedural program. I would have wanted to extend the Date class so I can call next_friday on any Date object and be returned the Date object of the next Friday. I'd then extend it further to add weekends_until on a Date object, perhaps returning a range or an array of dates inside the weekend instead of just the start date. That would have the advantage of being able to pull it into a Rails project and then all my Date columns having "Friday and Weekend support", or whatever.

One other refinement functionally: for most people weekends either start at 6pm on Friday or on a Saturday morning. The whole of Friday isn't included, which here it is. I'd want to tweak that. Going further, I know one guy who works 10 days on, 5 days off. Oil/gas rig workers work 2 weeks on and 2 weeks off. I think without a huge number of changes once you've extended Date or DateTime, it would be possible to return ranges of those - as appropriate, and then it becomes quite a seriously funky library to include in calendaring apps.

So, my only comments are in essence:

  1. Extend Date or DateTime
  2. Think a little wider outside of your definition of downtime and return ranges (or at least use ranges in the function and return an array of dates with .to_a)

Otherwise seems good to go! :-)

@tekin
Copy link

tekin commented Jun 6, 2011

I agree with Paul, the naming of your variables and methods could be improved and you should have tests as well!

I'm not sure I agree that you need to be extending any classes though, if this is a simple utility function that you're using in one project, then I'd follow the principle of YAGNI and leave it at that!

I've forked the code and tweaked it a bit to use some handy ruby/activesupport bits and renamed things so they say more what they mean.

There is some ambiguity in your intent regarding the end date. For example, if the end date was a Friday, then is that weekend included? In the current implementation, the answer is yes, but that may not be what you want. Tests would help clarify this.

@alterisian
Copy link
Author

Wow. Thanks for the feedback. Spent about 15 minutes writing it, and I agree about the tests for sure.
I suppose a detailed comment of what I was trying to achieve would have been useful too.
Initially for creating a list of weekends I can poll mates other availability for. Then, I thought perhaps it could be used to generate doddle.ch questionnaires, then I was thinking maybe I could just create my own app on the side. Either way I thought putting the code 'out there' may actually be of use for others.
Anyways, Thanks loads for the feedback. Wanting to keep my Ruby active, so this will help a lot. I will try to evaluate it properly, and refine it. Cheers :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment