From 4531fddc379bc2238e06a48e1ed7d33f479dbec0 Mon Sep 17 00:00:00 2001 From: Amey Kusurkar Date: Thu, 8 Sep 2022 21:16:52 +0100 Subject: [PATCH] Refactor `add_business_days` and `subtract_business_days` `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. --- lib/business/calendar.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/business/calendar.rb b/lib/business/calendar.rb index 0aab1f4..bba6989 100644 --- a/lib/business/calendar.rb +++ b/lib/business/calendar.rb @@ -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 @@ -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