无法在Ruby中迭代Time对象

时间:2014-09-03 17:22:27

标签: ruby time google-calendar-api

我正在写一个约会表格,让用户选择一个日期。然后,它将按照日期检查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

此方法需要一个日期并获取该日期每个事件的start_timeend_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

当我尝试迭代冲突数组时,会抛出'无法迭代时间'错误。我试图在冲突数组上调用to_enum,但仍然遇到同样的错误。

我在SO上看到的所有其他问题都引用了step方法,这似乎不适用于这种情况。

更新

Thanks @caryswoveland and @fivedigit. I combined both of your answers, which were very helpful for different aspects of my solution:

  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| DateTime.parse(event.start_time)..DateTime.parse(event.end_time) }
    results = resolve_time_conflicts(conflicts) 
  end

  def resolve_time_conflicts(conflicts)
    date = conflicts.first.first   
    start_time = DateTime.new(date.year, date.month, date.day, 10, 00).change(offset: date.zone) 
    available_times = [] 
    14.times do |interval_multiple|
      appointment_time = (start_time + ((interval_multiple * 30).minutes))
      available_times << appointment_time unless conflicts.any? { |conflict| conflict.cover?(appointment_time)}  
    end
      available_times 
  end

3 个答案:

答案 0 :(得分:6)

问题来自这一点:

(conflict[0]..conflict[1]).include?(appointment_time)
# TypeError: can't iterate from Time

您创建了一系列时间,然后检查appointment_time是否在该范围内。这就是导致您遇到错误的原因。

而不是include?,您应该使用cover?

(conflict[0]..conflict[1]).cover?(appointment_time)

这假定conflict[0]是最早的时间。

答案 1 :(得分:5)

<强>异常

@fivedigit解释了引发异常的原因。

其他问题

您需要any? each

appointment_times = []
  #=> []
appointment = 4
  #=> 4
conflicts = [(1..3), (5..7)]
  #=> [1..3, 5..7]

appointment_times << 5 unless conflicts.each { |r| r.cover?(appointment) }
  #=> nil
appointment_times
  #=> []

appointment_times << 5 unless conflicts.any? { |r| r.include?(appointment) }
  #=> [5]
appointment_times
  #=> [5]

我建议您将appointment_time隐藏到Time个对象,制作conflicts和元素数组[start_time, end_time],然后将appointment_time与端点进行比较:

...unless conflicts.any?{ |start_time, end_time|
     start_time <= appointment_time && appointment_time <= end_time }  

除此之外:Range#include?仅在端点为“数字”时查看端点(如Range#cover? does)。 Range#include?只需要在Time个对象时查看端点,但我不知道Ruby是否将Time个对象视为“数字”。我想可以查看源代码。有人知道吗?

替代方法

我想建议一种不同的方法来实现您的方法。我将以一个例子来做。

假设约会是15分钟,第一个区块是上午10:00至上午10:15,最后一个区域是下午4:45至下午5:00。 (块可能会更短,当然,持续时间只有1秒。)

让10:00 am-10:15 am为0区块,10:15 am-10:30 am为1区块,依此类推,直到第27区,下午4:45-5-5:00。

接下来,将conflicts表示为由[start, end]给出的块范围数组。假设有以下任命:

10:45am-11:30am (blocks 3, 4 and 5)
 1:00pm- 1:30pm (blocks 12 and 13)
 2:15pm- 3:30pm (blocks 17, 18 and 19)

然后:

conflicts = [[3,5], [12,13], [17,19]]

您必须编写一个返回reserved_blocks(appointment_date)的方法conflicts

其余代码如下:

BLOCKS = 28
MINUTES = ["00", "15", "30", "45"]
BLOCK_TO_TIME = (BLOCKS-1).times.map { |i|
  "#{i<12 ? 10+i/4 : (i-8)/4}:#{MINUTES[i%4]}#{i<8 ? 'am' : 'pm'}" }
  #=> ["10:00am", "10:15am", "10:30am", "10:45am",
  #    "11:00am", "11:15am", "11:30am", "11:45am",
  #    "12:00pm", "12:15pm", "12:30pm", "12:45pm",
  #     "1:00pm",  "1:15pm",  "1:30pm",  "1:45pm",
  #     "2:00pm",  "2:15pm",  "2:30pm",  "2:45pm",
  #     "3:00pm",  "3:15pm",  "3:30pm",  "3:45pm",
  #     "4:00pm",  "4:15pm",  "4:30pm",  "4:45pm"]

def available_times(appointment_date)
  available = [*(0..BLOCKS-1)]-reserved_blocks(appointment_date)
                .flat_map { |s,e| (s..e).to_a }
  last = -2 # any value will do, can even remove statement
  test = false
  available.chunk { |b| (test=!test) if b > last+1; last = b; test }
           .map { |_,a| [BLOCK_TO_TIME[a.first], 
             (a.last < BLOCKS-1) ? BLOCK_TO_TIME[a.last+1] : "5:00pm"] }
end

def reserved_blocks(date) # stub for demonstration.
  [[3,5], [12,13], [17,19]]
end

让我们看看我们得到了什么:

available_times("anything") 
  #=> [["10:00am", "10:45am"],
  #    ["11:30am",  "1:00pm"],
  #    [ "1:45pm",  "2:15pm"], 
  #    [ "3:00pm",  "5:00pm"]]

<强>解释

以下是发生的事情:

appointment_date = "anything" # dummy for demonstration

all_blocks = [*(0..BLOCKS-1)]
  #=> [ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13,
  #    14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27]
reserved_ranges = reserved_blocks(appointment_date)
  #=> [[3, 5], [12, 13], [17, 19]]
reserved = reserved_ranges.flat_map { |s,e| (s..e).to_a }
  #=> [3, 4, 5, 12, 13, 17, 18, 19]
available = ALL_BLOCKS - reserved
  #=> [0, 1, 2, 6, 7, 8, 9, 10, 11, 14, 15, 16, 20, 21, 22, 23, 24, 25, 26, 27]

last = -2
test = false
enum1 = available.chunk { |b| (test=!test) if b > last+1; last = b; test }
  #=> #<Enumerator: #<Enumerator::Generator:0x00000103063570>:each>

我们可以将它转换为数组,以查看如果map没有遵循,它将传递给块的值:

enum1.to_a
  #=> [[true, [0, 1, 2]],
  #    [false, [6, 7, 8, 9, 10, 11]],
  #    [true, [14, 15, 16]],
  #    [false, [20, 21, 22, 23, 24, 25, 26, 27]]]

Enumerable#chunk对枚举器的连续值进行分组。它是通过对test的值进行分组并在遇到非连续值时在truefalse之间翻转其值来实现的。

enum2 = enum1.map
  #=> #<Enumerator: #<Enumerator: (cont.)
      #<Enumerator::Generator:0x00000103063570>:each>:map>

enum2.to_a
  #=> [[true, [0, 1, 2]],
  #    [false, [6, 7, 8, 9, 10, 11]],
  #    [true, [14, 15, 16]],
  #    [false, [20, 21, 22, 23, 24, 25, 26, 27]]]

您可能会将enum2视为“复合”枚举器。

最后,我们转换传递给块的enum2的每个值的第二个元素(块变量a,对于传递的第一个元素等于[0,1,2])范围表示为12小时的时间。不使用enum2truefalse)的每个值的第一个元素,因此我用下划线替换了它的块变量。这提供了期望的结果:

enum2.each { |_,a|[BLOCK_TO_TIME[a.first], \
        (a.last < BLOCKS-1) ? BLOCK_TO_TIME[a.last+1] : "5:00pm"] }
  #=> [["10:00am", "10:45am"],
  #    ["11:30am",  "1:00pm"],
  #    [ "1:45pm",  "2:15pm"], 
  #    [ "3:00pm",  "5:00pm"]]

答案 2 :(得分:0)

将范围从一系列时间转换为整数范围:

range = (conflict[0].to_i..conflict[1].to_i)

然后在使用===

时使用include?运算符
conflict === appointment_time

编辑:您显然也可以将appointment_time转换为整数并仍然使用include?,因为范围现在只是一个整数范围。