Class: Zold::Age

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/age.rb

Overview

Age

Instance Method Summary collapse

Constructor Details

#initialize(time) ⇒ Age

Returns a new instance of Age.



32
33
34
# File 'lib/zold/age.rb', line 32

def initialize(time)
  @time = time.nil? || time.is_a?(Time) ? time : Time.parse(time)
end

Instance Method Details

#to_sObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/zold/age.rb', line 36

def to_s
  return '?' if @time.nil?
  sec = Time.now - @time
  if sec < 1
    "#{(sec * 1000).round}ms"
  elsif sec < 60
    "#{sec.round(2)}s"
  elsif sec < 60 * 60
    "#{(sec / 60).round}m"
  else
    "#{(sec / 3600).round}h"
  end
end