Class: TimeTally::Duration
- Inherits:
-
Object
- Object
- TimeTally::Duration
- Defined in:
- lib/time_tally/duration.rb
Instance Attribute Summary collapse
-
#h ⇒ Object
Returns the value of attribute h.
-
#m ⇒ Object
Returns the value of attribute m.
-
#s ⇒ Object
Returns the value of attribute s.
Instance Method Summary collapse
- #+(other) ⇒ Object
-
#initialize(str = nil) ⇒ Duration
constructor
A new instance of Duration.
- #normalize ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(str = nil) ⇒ Duration
Returns a new instance of Duration.
5 6 7 8 9 10 11 12 |
# File 'lib/time_tally/duration.rb', line 5 def initialize(str = nil) if str @h, @m, @s = str.split(":").map(&:to_i) normalize else @h = @m = @s = 0 end end |
Instance Attribute Details
#h ⇒ Object
Returns the value of attribute h.
3 4 5 |
# File 'lib/time_tally/duration.rb', line 3 def h @h end |
#m ⇒ Object
Returns the value of attribute m.
3 4 5 |
# File 'lib/time_tally/duration.rb', line 3 def m @m end |
#s ⇒ Object
Returns the value of attribute s.
3 4 5 |
# File 'lib/time_tally/duration.rb', line 3 def s @s end |
Instance Method Details
#+(other) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/time_tally/duration.rb', line 26 def +(other) result = Duration.new result.s = s + other.s result.m = m + other.m result.h = h + other.h result.normalize result end |
#normalize ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/time_tally/duration.rb', line 14 def normalize if @s > 59 @m += @s / 60 @s %= 60 end if @m > 59 @h += @m / 60 @m %= 60 end end |
#to_s ⇒ Object
35 36 37 |
# File 'lib/time_tally/duration.rb', line 35 def to_s "%d:%02d:%02d" % [h, m, s] end |