Skip to main content
added 64 characters in body; edited tags; edited title; edited tags
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Linter speaks of the following mistakes, how can this be fixed? Formatting uptime as a human-friendly time interval

# extend Time class for readable method
class Time
  def readable
    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

  private

  def uptime
    (Time.now - self).to_i
  end
end

Linter raises the following warnings. How can this be fixed?

  • TooManyStatements: Time#readable has approx 10 statements [https://github.com/troessner/reek/blob/master/docs/Too-Many-Statements.md]
  • Assignment Branch Condition size for readable is too high. [22.47/15]
  • Cyclomatic complexity for readable is too high. [9/6]
  • Method has too many lines. [11/10]

Linter speaks of the following mistakes, how can this be fixed?

# extend Time class for readable method
class Time
  def readable
    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

  private

  def uptime
    (Time.now - self).to_i
  end
end
  • TooManyStatements: Time#readable has approx 10 statements [https://github.com/troessner/reek/blob/master/docs/Too-Many-Statements.md]
  • Assignment Branch Condition size for readable is too high. [22.47/15]
  • Cyclomatic complexity for readable is too high. [9/6]
  • Method has too many lines. [11/10]

Formatting uptime as a human-friendly time interval

# extend Time class for readable method
class Time
  def readable
    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

  private

  def uptime
    (Time.now - self).to_i
  end
end

Linter raises the following warnings. How can this be fixed?

  • TooManyStatements: Time#readable has approx 10 statements [https://github.com/troessner/reek/blob/master/docs/Too-Many-Statements.md]
  • Assignment Branch Condition size for readable is too high. [22.47/15]
  • Cyclomatic complexity for readable is too high. [9/6]
  • Method has too many lines. [11/10]
Source Link

Linter speaks of the following mistakes, how can this be fixed?

# extend Time class for readable method
class Time
  def readable
    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

  private

  def uptime
    (Time.now - self).to_i
  end
end
  • TooManyStatements: Time#readable has approx 10 statements [https://github.com/troessner/reek/blob/master/docs/Too-Many-Statements.md]
  • Assignment Branch Condition size for readable is too high. [22.47/15]
  • Cyclomatic complexity for readable is too high. [9/6]
  • Method has too many lines. [11/10]