Class: AsciiTracker::HHMM
- Inherits:
-
Struct
- Object
- Struct
- AsciiTracker::HHMM
- Includes:
- Comparable
- Defined in:
- lib/asciitracker/hhmm.rb
Instance Attribute Summary collapse
-
#hours ⇒ Object
Returns the value of attribute hours.
-
#minutes ⇒ Object
Returns the value of attribute minutes.
Class Method Summary collapse
-
.parse(txt) ⇒ Object
“12:30”, “1:15” or “00:45” for hours and minutes or “1.5” for fractions of an hour notation.
Instance Method Summary collapse
-
#-(other) ⇒ Object
def -(other); self.to_f - other.to_f end def (other); self.to_f other.to_f end.
- #<=>(other) ⇒ Object
-
#initialize(*args) ⇒ HHMM
constructor
five ways to express 1 hour and 30 minutes: 1 arg: [“1:30”], [“1.5”] or [1.5] or a HHMM object to be cloned from 2 args: [1, 30], [“1”, “30”],.
- #to_a ⇒ Object
- #to_f ⇒ Object
- #to_minutes ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*args) ⇒ HHMM
five ways to express 1 hour and 30 minutes:
1 arg: ["1:30"], ["1.5"] or [1.5]
or a HHMM object to be cloned from
2 args: [1, 30], ["1", "30"],
40 41 42 43 44 45 46 47 48 |
# File 'lib/asciitracker/hhmm.rb', line 40 def initialize *args if 2 == args.length # [1, 30] or ["1", "30"] super *(args.map{ |e| Integer(e) }) else hhmm = args.first hhmm = HHMM.parse(hhmm.to_s) unless hhmm.kind_of? HHMM self.hours, self.minutes = hhmm.hours, hhmm.minutes end end |
Instance Attribute Details
#hours ⇒ Object
Returns the value of attribute hours
3 4 5 |
# File 'lib/asciitracker/hhmm.rb', line 3 def hours @hours end |
#minutes ⇒ Object
Returns the value of attribute minutes
3 4 5 |
# File 'lib/asciitracker/hhmm.rb', line 3 def minutes @minutes end |
Class Method Details
.parse(txt) ⇒ Object
“12:30”, “1:15” or “00:45” for hours and minutes
or
“1.5” for fractions of an hour notation
27 28 29 30 31 32 33 34 |
# File 'lib/asciitracker/hhmm.rb', line 27 def self.parse txt if (m = txt.match(/^(\d?\d):(\d\d)/)) HHMM.new m[1].to_i, m[2].to_i else minutes = ((Float(txt) * 60) + 0.5).to_i HHMM.new(minutes / 60, minutes % 60) end end |
Instance Method Details
#-(other) ⇒ Object
def -(other); self.to_f - other.to_f end def (other); self.to_f other.to_f end
13 14 15 16 17 |
# File 'lib/asciitracker/hhmm.rb', line 13 def -(other) m = to_minutes - other.to_minutes (m += 24 * 60) if m < 0 HHMM.new *(m.divmod 60) end |
#<=>(other) ⇒ Object
6 7 8 9 |
# File 'lib/asciitracker/hhmm.rb', line 6 def <=>(other) a = (hours <=> other.hours) a == 0 ? (minutes <=> other.minutes) : a end |
#to_a ⇒ Object
19 |
# File 'lib/asciitracker/hhmm.rb', line 19 def to_a; [hours, minutes] end |
#to_f ⇒ Object
21 |
# File 'lib/asciitracker/hhmm.rb', line 21 def to_f; (minutes.to_f/60) + hours end |
#to_minutes ⇒ Object
22 |
# File 'lib/asciitracker/hhmm.rb', line 22 def to_minutes; (60*hours) + minutes end |
#to_s ⇒ Object
20 |
# File 'lib/asciitracker/hhmm.rb', line 20 def to_s; "%02d:%02d" % [hours, minutes] end |