Skip to content

Commit

Permalink
Refactor add_business_days and subtract_business_days
Browse files Browse the repository at this point in the history
`add_business_days` and `subtract_business_days` are implemented by
repeatedly applying `next_business_day` and `previous_business_day`
respectively.

The code is identical. This refactor makes the intent  more explicit, improving readability.
  • Loading branch information
ameykusurkar authored and JoeSouthan committed Mar 31, 2023
1 parent f2f9dc1 commit 4531fdd
Showing 1 changed file with 2 additions and 8 deletions.
10 changes: 2 additions & 8 deletions lib/business/calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ def previous_business_day(date)
def add_business_days(date, delta)
date = roll_forward(date)
delta.times do
loop do
date += day_interval_for(date)
break date if business_day?(date)
end
date = next_business_day(date)
end
date
end
Expand All @@ -145,10 +142,7 @@ def add_business_days(date, delta)
def subtract_business_days(date, delta)
date = roll_backward(date)
delta.times do
loop do
date -= day_interval_for(date)
break date if business_day?(date)
end
date = previous_business_day(date)
end
date
end
Expand Down

0 comments on commit 4531fdd

Please sign in to comment.