Class: HMS::Duration
- Inherits:
-
Numeric
- Object
- Numeric
- HMS::Duration
- Extended by:
- Forwardable
- Includes:
- Comparable
- Defined in:
- lib/hms/duration.rb
Overview
Represents durations in hh:mm:ss format
Parts taken from gist.github.com/309694
Instance Method Summary collapse
- #%(other) ⇒ Object
- #*(other) ⇒ Object
- #**(other) ⇒ Object
- #+(other) ⇒ Object
- #+@ ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #coerce(other) ⇒ Object
- #eql?(other) ⇒ Boolean
-
#hours ⇒ Object
returns the hours part of the total duration.
-
#initialize(val = 0) ⇒ Duration
constructor
A new instance of Duration.
-
#minutes ⇒ Object
returns the hours part of the total duration.
-
#seconds ⇒ Object
Returns the seconds part of the total duration.
-
#to_f ⇒ Object
returns the total duration in seconds.
-
#to_i ⇒ Object
returns the total duration in seconds.
- #to_s ⇒ Object
Constructor Details
#initialize(val = 0) ⇒ Duration
Returns a new instance of Duration.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/hms/duration.rb', line 10 def initialize(val = 0) case val when String @seconds = parse_string(val) when Numeric @seconds = val.to_i when nil @seconds = 0 else raise ArgumentError, "Cannot convert #{val} to Duration" end end |
Instance Method Details
#%(other) ⇒ Object
121 122 123 |
# File 'lib/hms/duration.rb', line 121 def %(other) op(:%, other) end |
#*(other) ⇒ Object
109 110 111 |
# File 'lib/hms/duration.rb', line 109 def *(other) op(:*, other) end |
#**(other) ⇒ Object
113 114 115 |
# File 'lib/hms/duration.rb', line 113 def **(other) op(:**, other) end |
#+(other) ⇒ Object
101 102 103 |
# File 'lib/hms/duration.rb', line 101 def +(other) op(:+, other) end |
#+@ ⇒ Object
93 94 95 |
# File 'lib/hms/duration.rb', line 93 def +@ self end |
#-(other) ⇒ Object
105 106 107 |
# File 'lib/hms/duration.rb', line 105 def -(other) op(:-, other) end |
#/(other) ⇒ Object
117 118 119 |
# File 'lib/hms/duration.rb', line 117 def /(other) op(:/, other) end |
#<=>(other) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/hms/duration.rb', line 81 def <=>(other) case other when Duration @seconds <=> other.to_i when Numeric @seconds <=> other else a, b = other.coerce(self) a <=> b end rescue nil end |
#==(other) ⇒ Object
71 72 73 |
# File 'lib/hms/duration.rb', line 71 def ==(other) @seconds == other.to_i end |
#coerce(other) ⇒ Object
23 24 25 |
# File 'lib/hms/duration.rb', line 23 def coerce(other) [Duration.new(other.to_i), self] end |
#eql?(other) ⇒ Boolean
67 68 69 |
# File 'lib/hms/duration.rb', line 67 def eql?(other) self.class.equal?(other.class) && @seconds == other.to_i end |
#hours ⇒ Object
returns the hours part of the total duration
44 45 46 |
# File 'lib/hms/duration.rb', line 44 def hours @seconds.abs / 60 / 60 * (@seconds < 0 ? -1 : 1) end |
#minutes ⇒ Object
returns the hours part of the total duration
51 52 53 |
# File 'lib/hms/duration.rb', line 51 def minutes (@seconds.abs / 60) % 60 * (@seconds < 0 ? -1 : 1) end |
#seconds ⇒ Object
Returns the seconds part of the total duration. For the total in seconds use #to_i.
59 60 61 |
# File 'lib/hms/duration.rb', line 59 def seconds @seconds.abs % 60 * (@seconds < 0 ? -1 : 1) end |
#to_f ⇒ Object
returns the total duration in seconds
37 38 39 |
# File 'lib/hms/duration.rb', line 37 def to_f @seconds.to_f end |
#to_i ⇒ Object
returns the total duration in seconds
30 31 32 |
# File 'lib/hms/duration.rb', line 30 def to_i @seconds end |
#to_s ⇒ Object
63 64 65 |
# File 'lib/hms/duration.rb', line 63 def to_s "#{'-' if @seconds < 0}#{'%02d' % hours.abs}:#{'%02d' % minutes.abs}:#{'%02d' % seconds.abs}" end |