Here I am trying to skip predefined holidays in an array, but the loop in infinite.
@holidays = [DateTime.new(2016,03,15), DateTime.new(2016,03,17)]
def skip_holidays(date)
date += 1
while @holidays.map{ |holiday| holiday == date} do
date = date.next_day.change(hour: 9)
end
date
end
skip_holidays(DateTime.new(2016,3,14))
However, this loop here works perfectly
def skip_weekends(date)
date += 1
while (date.saturday?) || (date.sunday?) do
date = date.next_day.change(hour: 9)
end
date
end
skip_weekends(DateTime.new(2016,3,14))
How is that and how to go around it?
Try this one:
@holidays = [DateTime.new(2016,03,15), DateTime.new(2016,03,17)]
def skip_holidays(date)
if @holidays.include?(date)
date = date.next_day.change(hour: 9)
else
date += 1
end
end
skip_holidays(DateTime.new(2016,3,14))