给定一个连续日期数组 dates = (1..15).map { |num| Date.today + num
如何删除星期日但通过为每个删除的星期日在末尾添加额外的一天来保留原始数组长度?
这是我的尝试,结果导致“堆栈级别太深”异常。
def remove_sundays(dates)
working_dates = dates.reject(&:sunday?)
size_diff = dates.size - working_dates.size
unless size_diff.zero?
last_day = working_dates.last
working_dates = working_dates + ((1..size_diff).map { |num| last_day + num })
remove_sundays(working_dates)
else
working_dates
end
end
最佳答案
很难。
def remove_sundays(dates)
dates.each.with_index do |d, i|
if d.sunday?
dates[i..-1] = dates[i..-1].map(&:next_day)
end
end
end
关于ruby - 从日期数组中删除星期日,但调整以删除星期日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21162264/