Class: Hours::Time
- Inherits:
-
Object
- Object
- Hours::Time
- Defined in:
- lib/hours/time.rb
Instance Attribute Summary collapse
-
#hours ⇒ Object
Returns the value of attribute hours.
-
#minutes ⇒ Object
Returns the value of attribute minutes.
Instance Method Summary collapse
- #-(subtrahend) ⇒ Object
-
#initialize(str_time) ⇒ Time
constructor
A new instance of Time.
- #to_hours ⇒ Object
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
#hours ⇒ Object
Returns the value of attribute hours.
4 5 6 |
# File 'lib/hours/time.rb', line 4 def hours @hours end |
#minutes ⇒ Object
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_hours ⇒ Object
20 21 22 |
# File 'lib/hours/time.rb', line 20 def to_hours hours + minutes / 60.0 end |