Class: Hours::Time

Inherits:
Object
  • Object
show all
Defined in:
lib/hours/time.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str_time) ⇒ Time

Returns a new instance of Time.



6
7
8
9
10
11
12
13
# File 'lib/hours/time.rb', line 6

def initialize(str_time)
  if (str_time =~ /(\d{1,2}):(\d{2})/)
    self.hours   = $1.to_i
    self.minutes = $2.to_i
  else
    raise ArgumentError, 'argument must be in format "hh:mm"'
  end
end

Instance Attribute Details

#hoursObject

Returns the value of attribute hours.



4
5
6
# File 'lib/hours/time.rb', line 4

def hours
  @hours
end

#minutesObject

Returns the value of attribute minutes.



4
5
6
# File 'lib/hours/time.rb', line 4

def minutes
  @minutes
end

Instance Method Details

#-(subtrahend) ⇒ Object



15
16
17
18
# File 'lib/hours/time.rb', line 15

def -(subtrahend)
  result = self.to_hours - subtrahend.to_hours
  (result < 0) ? (24 + result) : result
end

#to_hoursObject



20
21
22
# File 'lib/hours/time.rb', line 20

def to_hours
  hours + minutes / 60.0
end