Class: Timesplit::Split
- Inherits:
-
Object
- Object
- Timesplit::Split
- Defined in:
- lib/timesplit/split.rb
Constant Summary collapse
- SECS =
60.0
Class Method Summary collapse
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #==(other) ⇒ Object
- #coerce(other) ⇒ Object
- #hours ⇒ Object
-
#initialize(*times) ⇒ Split
constructor
A new instance of Split.
- #minutes ⇒ Object
- #seconds ⇒ Object
- #to_f ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*times) ⇒ Split
Returns a new instance of Split.
10 11 12 13 14 15 16 |
# File 'lib/timesplit/split.rb', line 10 def initialize(*times) times = times.first.split(/[^\d]+/).map(&:to_f) if times.first.is_a? String multiplier = 1 @seconds = times.reverse.reduce do |t, n| t += n * (multiplier *= SECS) end end |
Class Method Details
.[](*values) ⇒ Object
6 7 8 |
# File 'lib/timesplit/split.rb', line 6 def self.[](*values) new(*values) end |
Instance Method Details
#*(other) ⇒ Object
66 67 68 |
# File 'lib/timesplit/split.rb', line 66 def *(other) Split.new(@seconds * other.to_f) end |
#+(other) ⇒ Object
54 55 56 |
# File 'lib/timesplit/split.rb', line 54 def +(other) Split.new(@seconds + other.to_f) end |
#-(other) ⇒ Object
58 59 60 |
# File 'lib/timesplit/split.rb', line 58 def -(other) Split.new(@seconds - other.to_f) end |
#/(other) ⇒ Object
62 63 64 |
# File 'lib/timesplit/split.rb', line 62 def /(other) Split.new(@seconds / other.to_f) end |
#==(other) ⇒ Object
46 47 48 |
# File 'lib/timesplit/split.rb', line 46 def ==(other) self.to_f == other.to_f end |
#coerce(other) ⇒ Object
50 51 52 |
# File 'lib/timesplit/split.rb', line 50 def coerce(other) [Split.new(other.to_f), self] end |
#hours ⇒ Object
18 19 20 |
# File 'lib/timesplit/split.rb', line 18 def hours calculate_parts[0] end |
#minutes ⇒ Object
22 23 24 |
# File 'lib/timesplit/split.rb', line 22 def minutes calculate_parts[1] end |
#seconds ⇒ Object
26 27 28 |
# File 'lib/timesplit/split.rb', line 26 def seconds calculate_parts[2] end |
#to_f ⇒ Object
42 43 44 |
# File 'lib/timesplit/split.rb', line 42 def to_f @seconds.to_f end |
#to_i ⇒ Object
38 39 40 |
# File 'lib/timesplit/split.rb', line 38 def to_i @seconds.to_i end |
#to_s ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/timesplit/split.rb', line 30 def to_s [ hours.to_i.to_s.rjust(2, '0'), minutes.to_i.to_s.rjust(2, '0'), seconds.to_i.to_s.rjust(2, '0'), ].join(':') end |