Class: AsciiTracker::HHMM

Inherits:
Struct
  • Object
show all
Includes:
Comparable
Defined in:
lib/asciitracker/hhmm.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#hoursObject

Returns the value of attribute hours

Returns:

  • (Object)

    the current value of hours



3
4
5
# File 'lib/asciitracker/hhmm.rb', line 3

def hours
  @hours
end

#minutesObject

Returns the value of attribute minutes

Returns:

  • (Object)

    the current value of 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_aObject



19
# File 'lib/asciitracker/hhmm.rb', line 19

def to_a; [hours, minutes] end

#to_fObject



21
# File 'lib/asciitracker/hhmm.rb', line 21

def to_f; (minutes.to_f/60) + hours end

#to_minutesObject



22
# File 'lib/asciitracker/hhmm.rb', line 22

def to_minutes; (60*hours) + minutes end

#to_sObject



20
# File 'lib/asciitracker/hhmm.rb', line 20

def to_s; "%02d:%02d" % [hours, minutes] end