我正在写一个约会表格,让用户选择一个日期.然后,它将按照日期检查Google日历,在上午10:00到下午5:00之间的30分钟时间间隔内查看该日期的可用时段.
在我的Calendar类中,我有一个available_times方法:
def available_times(appointment_date)
appointment_date_events = calendar.events.select { |event| Date.parse(event.start_time) == appointment_date }
conflicts = appointment_date_events.map { |event| [Time.parse(event.start_time), Time.parse(event.end_time)] }
results = resolve_time_conflicts(conflicts)
end
Run Code Online (Sandbox Code Playgroud)
此方法需要一个日期和抓住start_time并end_time在该日期每个事件.然后它调用resolve_time_conflicts(conflicts):
def resolve_time_conflicts(conflicts)
start_time = Time.parse('10:00am')
available_times = []
14.times do |interval_multiple|
appointment_time = (start_time + interval_multiple * (30 * 60))
available_times << appointment_time unless conflicts.each{ |conflict| (conflict[0]..conflict[1]).include?(appointment_time)}
end
available_times
end
Run Code Online (Sandbox Code Playgroud)
当我尝试迭代冲突数组时,会抛出'无法迭代时间'错误.我试图调用to_enum冲突数组但仍然得到相同的错误.
我在SO上看到的所有其他问题都是引用该step方法,这似乎不适用于这种情况.
更新:
Thanks @caryswoveland and @fivedigit. I combined both of your …Run Code Online (Sandbox Code Playgroud)