如何重构案例......在Ruby中

时间:2017-09-18 09:44:42

标签: ruby refactoring rubocop reek

  def readable
    uptime = (Time.now - self).to_i
    case uptime
    when 0 then 'just now'
    when 1 then 'uptime second ago'
    when 2..59 then uptime.to_s + ' seconds ago'
    when 60..119 then 'uptime minute ago' # 120 = 2 minutes
    when 120..3540 then (uptime / 60).to_i.to_s + ' minutes ago'
    when 3541..7100 then 'an hour ago' # 3600 = 1 hour
    when 7101..82_800 then ((uptime + 99) / 3600).to_i.to_s + ' hours ago'
    when 82_801..172_000 then 'uptime day ago' # 86400 = 1 day
    else ((uptime + 800) / 86_400).to_i.to_s + ' days ago'
    end
  end

Linter谈到以下错误,如何解决?

1 个答案:

答案 0 :(得分:0)

查看time_ago_in_words并随意使用它或其代码。

关于代码指标 - 您的代码非常简单,您应该只提取uptime方法。