Class: Zold::Age
- Inherits:
-
Object
- Object
- Zold::Age
- Defined in:
- lib/zold/age.rb
Overview
Age
Instance Method Summary collapse
-
#initialize(time) ⇒ Age
constructor
A new instance of Age.
- #to_s ⇒ Object
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_s ⇒ Object
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 |