请帮我这个Ruby代码

时间:2010-06-07 15:03:01

标签: ruby refactoring

我是Ruby的新手,我知道我没有使用其中可用的简单而强大的方法。我让我的代码工作,但它必须简单而不是这么大(特别是我觉得我在循环变量上非常糟糕)

i = 0
j = 0

loop_count = ((to_date-from_date)/(60*60*24)).to_i#diff b/w two dates in days

loop_count.times do
   48.times do

    event = Icalendar::Event.new

    status = get_availability_on_date_and_hour(@templates, @availabilities, from_date+j.days, i).downcase

    if(status != 'unavailable')
      #Initialize start and end dates in DateTime's civil format
      bias_date_time = DateTime.civil(from_date.year, from_date.month, from_date.day)

      event.dtstart = bias_date_time + j.day + (i/2).to_i.hour + (i%2*30).to_i.minutes
      event.dtend = event.dtstart + 30.minutes

      event.summary = status.upcase
      cal.add_event(event)
    end

    i += 1
  end
  i = 0
  j += 1
end

3 个答案:

答案 0 :(得分:4)

没有什么特别针对Ruby的,但总的来说,你的方法很简短..

我会将循环内容提取到一个单独的方法中。方法名称用于描述正在发生的事情。

我还要将计算提取到一个单独的方法来描述你正在尝试做什么,并保持复杂的位(最可能需要更改的部分)分开..

类似的东西:

0.upto(((to_date-from_date)/(60*60*24)).to_i) do |j|
  0.upto(48) do |i|
      status = get_availability_on_date_and_hour(@templates, @availabilities, from_date+j.days, i).downcase

      add_event(from_date, to_date, i, j, status) if status != 'unavailable'
  end
end


def add_event(from_date, to_date, i, j, status)
      event = Icalendar::Event.new

      event.dtstart = whatever_i_am_trying_to_calculate(from_date, i, j)
      event.dtend = event.dtstart + 30.minutes
      event.summary = status.upcase

      cal.add_event(event)
end

def whatever_i_am_trying_to_calculate(from_date, i, j)
  bias_date_time = DateTime.civil(from_date.year, from_date.month, from_date.day)
  bias_date_time + j.day + (i/2).to_i.hour + (i%2*30).to_i.minutes
end

同时为变量i和j提供有用的名称。我和j并不是很有意思。

答案 1 :(得分:2)

您可以像这样摆脱计数器变量ij的初始化和递增:

0.upto(((to_date-from_date)/(60*60*24)).to_i) do |j|
  0.upto(48) do |i|
    event = Icalendar::Event.new

    status = get_availability_on_date_and_hour(@templates, @availabilities, from_date+j.days, i).downcase

    if(status != 'unavailable')
      #Initialize start and end dates in DateTime's civil format
      bias_date_time = DateTime.civil(from_date.year, from_date.month, from_date.day)

      event.dtstart = bias_date_time + j.day + (i/2).to_i.hour + (i%2*30).to_i.minutes
      event.dtend = event.dtstart + 30.minutes

      event.summary = status.upcase
      cal.add_event(event)
    end
  end
end

这段代码比你的短6行,对循环中的代码无能为力,因为我真的不明白你在那里做什么。

更新:另类也适用:

((to_date-from_date)/(60*60*24)).to_i.times do |j|
  48.times do |i|
    event = Icalendar::Event.new

    status = get_availability_on_date_and_hour(@templates, @availabilities, from_date+j.days, i).downcase

    if(status != 'unavailable')
      #Initialize start and end dates in DateTime's civil format
      bias_date_time = DateTime.civil(from_date.year, from_date.month, from_date.day)

      event.dtstart = bias_date_time + j.day + (i/2).to_i.hour + (i%2*30).to_i.minutes
      event.dtend = event.dtstart + 30.minutes

      event.summary = status.upcase
      cal.add_event(event)
    end
  end
end

答案 2 :(得分:1)

我们可以摆脱j变量并避免在任何地方添加j.days并使用date

from_date.to_i.step(to_date.to_i, 24*60*60) do |date|
  0.upto(48) do |half_hour|
    each_date = Time.at(date)
    event = Icalendar::Event.new

    status = get_availability_on_date_and_hour(@templates, @availabilities, each_date, half_hour).downcase

    if(status != 'unavailable')
      #Initialize start and end dates in DateTime's civil format

      bias_date_time = DateTime.civil(each_date.year, each_date.month, each_date.day)

      event.dtstart = bias_date_time + (i/2).to_i.hour + (i%2*30).to_i.minutes
      event.dtend = event.dtstart + 30.minutes

      event.summary = status.upcase
      cal.add_event(event)
    end
  end
end