add_weekdays method

C

Chris Roos

Hi,

I've just spent some time looking at an add_weekdays method on the Date
class.

My implementation is as follows.

class Date
def add_weekdays(days)
new_date = self
while days > 0
new_date += 1
days -= 1 unless (new_date.wday == 6 || new_date.wday == 0)
end
new_date
end
end

I'm interested to know whether I've a) wasted my time because I could
have achieved the same thing with an already available object/method b)
commited any ruby sins and c) whether there are much better ways of
implementing this?

Chris
 
T

Todd

Chris said:
Hi,

I've just spent some time looking at an add_weekdays method on the Date
class.

My implementation is as follows.

class Date
def add_weekdays(days)
new_date = self
while days > 0
new_date += 1
days -= 1 unless (new_date.wday == 6 || new_date.wday == 0)
end
new_date
end
end

I'm interested to know whether I've a) wasted my time because I could
have achieved the same thing with an already available object/method b)
commited any ruby sins and c) whether there are much better ways of
implementing this?

Chris

Looks like you want to skip weekends. I might do it this way...

class Date
def add_weekdays(days)
new_date = self
days.times { new_date += ( new_date.wday == 6 ? 3 : 1 ) }
end
end

But your way is just fine.

Todd
 
T

Todd

Todd said:
Looks like you want to skip weekends. I might do it this way...

class Date
def add_weekdays(days)
new_date = self
days.times { new_date += ( new_date.wday == 6 ? 3 : 1 ) }
end
end

Don't listen to that piece of code. I thought I tested it, but now
that I try it, it fails. Here we go:

class Date
def add_weekdays(days)
new_date = self
days.times { new_date += ( new_date.wday==5 ? 3 ( new_date.wday==6
? 2 : 1 ) ) }
return new_date
end
end

puts Date.today.add_weekdays(4)
#or whatever number

Basically, if it's Friday, add 3, if it's Saturday add 2, otherwise add
1.

Like I said, though, your loop algorithm should work well.

Todd
 
C

Chris Roos

Todd said:
Don't listen to that piece of code. I thought I tested it, but now
that I try it, it fails. Here we go:

class Date
def add_weekdays(days)
new_date = self
days.times { new_date += ( new_date.wday==5 ? 3 ( new_date.wday==6
? 2 : 1 ) ) }
return new_date
end
end

puts Date.today.add_weekdays(4)
#or whatever number

Basically, if it's Friday, add 3, if it's Saturday add 2, otherwise add
1.

Like I said, though, your loop algorithm should work well.

Todd
Thanks for the alternative implementation Todd. I'm still running with
my original but I've changed it ever so slightly, as it's now in the
Time class rather than Date, and therefore I'm first creating a new
DateTime and then adding days to it.

def add_weekdays(days)
new_date = DateTime.new(self.year, self.month, self.day, self.hour,
self.min, self.sec)
while days > 0
new_date += 1
days -= 1 unless (new_date.wday == 6 || new_date.wday == 0)
end
new_date
end

I'm pretty certain it doesn't, but, on the off chance it makes a
difference this is to be used in a Rails app.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top