1

以下のコードは、タスクを完了するための営業時間を計算するのに役立ちます。このcalculate_deadlineメソッドでは、BusinesHours クラスで、2 つの 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>

特に「to_date」メソッドがクラス Time の一部であるため、2 つの puts ステートメントが示唆するように、calculate_deadline の「self」の横に start_time.to_date を配置すると、self が BusinessHours クラスから TimeRange クラスに変更される理由がわかりません。これがどのように起こっているか説明できますか?

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、つまり falsey になります)。

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

@modified_days[anything]はfalseyなので、上range@default_range見たようにTimeRangeオブジェクトである になります。メソッド[]onは、オブジェクトである変数をBusinessHours返します。rangeTimeRange

したがって、オブジェクトでselfあるため、メソッド ( )BusinessHoursを呼び出すと、オブジェクトが取得されます。[]self[argument]TimeRange

于 2012-11-14T05:27:58.910 に答える
0

It does not change your self. You are inspecting a different object.

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 に答える