class AsciiTracker::HHMM
Public Class Methods
new(*args)
click to toggle source
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"],
Calls superclass method
# 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
parse(txt)
click to toggle source
“12:30”, “1:15” or “00:45” for hours and minutes
or
“1.5” for fractions of an hour notation
# 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
Public Instance Methods
-(other)
click to toggle source
def -(other); self.to_f - other.to_f end def +(other); self.to_f + other.to_f end
# 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)
click to toggle source
# File lib/asciitracker/hhmm.rb, line 6 def <=>(other) a = (hours <=> other.hours) a == 0 ? (minutes <=> other.minutes) : a end
to_a()
click to toggle source
# File lib/asciitracker/hhmm.rb, line 19 def to_a; [hours, minutes] end
to_f()
click to toggle source
# File lib/asciitracker/hhmm.rb, line 21 def to_f; (minutes.to_f/60) + hours end
to_minutes()
click to toggle source
# File lib/asciitracker/hhmm.rb, line 22 def to_minutes; (60*hours) + minutes end
to_s()
click to toggle source
# File lib/asciitracker/hhmm.rb, line 20 def to_s; "%02d:%02d" % [hours, minutes] end