4236ea6b84f4899b469e8edff4bf6d22

Line 1 I'm grabbing the most 4 recent week ending dates (the date is a Friday).
Line 2 I'm finding the last date it found and finding the date for the next Friday.
Line 3 Appending to the array.

I'm using this data to build a simple chart so I have both some real data and then I'm projecting some values with the future dates.

Would there be a better way to build this array? It feels a little clunky...

1
2
3
4
5
6
@week_endings ||= Actual.get_week_endings(4.weeks.ago.to_date)
6.times do |p|
  actual = Actual.new
  actual.week_ending = @week_endings.last.week_ending.next_week(:friday)
  @week_endings << actual
end

Refactorings

No refactoring yet !

5170ca260dbd2cdfd5a887a4dba7636f

Jeremy Weiskotten

April 3, 2008, April 03, 2008 19:55, permalink

No rating. Login to rate!

I think you can simplify it with the assumption that there are 7 days in every week. This way you don't have to look up the last value in the array in your loop. It might perform a little better, if that's a concern, but I haven't profiled it.

1
2
3
4
5
6
7
@week_endings ||= Actual.get_week_endings(4.weeks.ago.to_date)
last = @week_endings.last.week_ending
6.times do |i|
  actual = Actual.new
  actual.week_ending = last + (7*(i+1))
  @week_endings << actual
end
F04aeb28129f653b207e8b5d92706096

Adkron

April 23, 2008, April 23, 2008 12:17, permalink

No rating. Login to rate!

get rid of the last+7*(i+1)) and go with the good old Time methods.

1
2
3
4
5
6
7
@week_endings ||= Actual.get_week_endings(4.weeks.ago.to_date)
last = @week_endings.last.week_ending
6.times do |i|
  actual = Actual.new
  actual.week_ending = i.weeks.from(last)
  @week_endings << actual
end

Your refactoring





Format Copy from initial code

or Cancel