1

下面的代码有助于计算完成任务的工作时间。在calculate_deadline方法中,在 BusinesHours 类中,两个 puts 语句揭示了这一点

 puts self
#<BusinessHours:0x000001008cbb70>

  puts self[start_time.to_date].inspect
#<TimeRange:0x000001008cbb48 @range=2010-06-10 09:00:00 -0700..2010-06-10 15:00:00 -0700>

我不明白为什么在calculate_deadline 中将start_time.to_date 放在'self' 旁边会将self 从BusinessHours 类更改为TimeRange 类,正如两个puts 语句所暗示的那样,特别是因为'to_date' 方法是Time 类的一部分。你能解释一下这是怎么发生的吗?

require 'time'
require 'date'

class Time
  def to_date
    Date.new(year, month, day)
  end
end

class BusinessHours

  def initialize(time_in, time_out)
    @default_range = TimeRange.new(time_in, time_out)
    @modified_days = {}
  end

  def update(day, time_in, time_out)
    key = day.is_a?(Symbol) ? day : Date.parse(day)
    @modified_days.merge!({key => TimeRange.new(time_in, time_out)})
  end

  def closed(*days)
    days.each {|day| update(day, '0:00', '0:00')}      
  end

  def [](date)
    day_of_week = date.strftime("%a").downcase.to_sym
    range = @modified_days[date] || @modified_days[day_of_week] || @default_range
    # reset time range dates to match date param
    range.reset_date(date)
    range
  end

  def calculate_deadline(seconds, start_time)
    start_time = Time.parse(start_time)
    puts self
    puts self[start_time.to_date].inspect
    range = self[start_time.to_date]


    if range.applies?(start_time)
      start_time = [start_time, range.start].max
      available_seconds = range.stop - start_time

      return start_time + seconds if available_seconds > seconds
      seconds -= available_seconds
    end

    calculate_deadline(seconds, (start_time.to_date + 1).to_s)
  end

end

class TimeRange

  def initialize(time_in, time_out)
    @range = Time.parse(time_in)..Time.parse(time_out)

  end

  def reset_date(date)
    @range = Time.local(date.year, date.month, date.day, start.hour, start.min)..
      Time.local(date.year, date.month, date.day, stop.hour, stop.min)    
  end

  def applies?(time)
    stop > time
  end

  def stop

    @range.end
  end

  def start
    @range.begin
  end

end

k = BusinessHours.new("9:00 AM", "3:00 PM")
k.calculate_deadline(20*60*60, "Jun 7, 2010 10:45 AM")
4

2 回答 2

2
@default_range = TimeRange.new(time_in, time_out)

从那行,我们可以看到这@default_range是一个TimeRange实例,它@modified_days是一个空哈希(因此@modified_days[anything]将为 nil,即假)。

range = @modified_days[date] || @modified_days[day_of_week] || @default_range

由于@modified_days[anything]是假的,range最终成为@default_range,正如我们在上面看到的,它是一个TimeRange对象。您的[]方法BusinessHours返回range变量,它是一个TimeRange对象。

因此,self作为一个BusinessHours对象,当你调用[]方法(self[argument])时,你会得到一个TimeRange对象。

于 2012-11-14T05:27:58.910 回答
0

它不会改变您的self. 您正在检查不同的对象。

self.inspect       # inspecting 'self'.

self[date].inspect # inspecting 'self[date]'
                   # which returns a `TimeRange` object `range`.
                   # Hence, you are inspecting `range`.
于 2012-11-14T05:27:01.983 回答