Class: Workpattern::Clock
- Inherits:
-
Object
- Object
- Workpattern::Clock
- Defined in:
- lib/workpattern/clock.rb
Overview
Represents time on a clock in hours and minutes.
Instance Method Summary collapse
-
#hour ⇒ Integer
Returns the hour of the clock (0-23).
-
#initialize(hour = 0, min = 0) ⇒ Clock
constructor
Initialises an instance of
Clock
using the hours and minutes supplied or 0 if they are absent. -
#min ⇒ Integer
Returns the minute of the clock (0-59).
-
#minutes ⇒ Integer
Returns the total number of minutes.
-
#time ⇒ DateTime
Returns a
Time
object with the correcthour
andmin
values. -
#to_s ⇒ String
Representation of
Clock
value as ‘hh:mn minutes’.
Constructor Details
#initialize(hour = 0, min = 0) ⇒ Clock
Initialises an instance of Clock
using the hours and minutes supplied or 0 if they are absent. Although there are 24 hours in a day (0-23) and 60 minutes in an hour (0-59), Clock
calculates the full hours and remaining minutes of whatever is supplied.
30 31 32 33 |
# File 'lib/workpattern/clock.rb', line 30 def initialize(hour = 0, min = 0) @hour = total_minutes(hour, min).div(60) @min = total_minutes(hour, min) % 60 end |
Instance Method Details
#hour ⇒ Integer
Returns the hour of the clock (0-23)
47 48 49 |
# File 'lib/workpattern/clock.rb', line 47 def hour @hour % 24 end |
#min ⇒ Integer
Returns the minute of the clock (0-59)
55 56 57 |
# File 'lib/workpattern/clock.rb', line 55 def min @min % 60 end |
#minutes ⇒ Integer
Returns the total number of minutes
39 40 41 |
# File 'lib/workpattern/clock.rb', line 39 def minutes total_minutes(@hour, @min) end |
#time ⇒ DateTime
Returns a Time
object with the correct hour
and min
values. The date is 10th June 1963.
64 65 66 |
# File 'lib/workpattern/clock.rb', line 64 def time DateTime.new(1963, 6, 10, hour, min) end |
#to_s ⇒ String
Returns representation of Clock
value as ‘hh:mn minutes’.
69 70 71 |
# File 'lib/workpattern/clock.rb', line 69 def to_s hour.to_s.concat(':').concat(min.to_s).concat(' ').concat(minutes.to_s) end |