Created
June 5, 2011 18:32
-
-
Save alterisian/1009253 to your computer and use it in GitHub Desktop.
Weekends left til the end of summer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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} |
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
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.