class Workpattern::Clock

Represents time on a clock in hours and minutes.

@example

myClock=Clock.new(3,32)
myClock.minutes #=> 212
myClock.hour #=> 3
myClock.min  #=> 32
myClock.time #=> Time.new(1963,6,10,3,32)
myClock.to_s #=> 3:32 212

aClock=Clock.new(27,80)
aClock.minutes #=> 1700
aClock.hour #=> 4
aClock.min #=> 20
aClock.time #=> Time.new(1963,6,10,4,20)
aClock.to_s #=> 4:20 1700

@since 0.2.0

Public Class Methods

new(hour = 0, min = 0) click to toggle source

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.

@param [Integer] hour number of hours @param [Integer] min number of minutes

# 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

Public Instance Methods

hour() click to toggle source

Returns the hour of the clock (0-23)

@return [Integer] hour of Clock from 0 to 23.

# File lib/workpattern/clock.rb, line 47
def hour
  @hour % 24
end
min() click to toggle source

Returns the minute of the clock (0-59)

@return [Integer] minute of Clock from 0 to 59

# File lib/workpattern/clock.rb, line 55
def min
  @min % 60
end
minutes() click to toggle source

Returns the total number of minutes

@return [Integer] total minutes represented by the Clock object

# File lib/workpattern/clock.rb, line 39
def minutes
  total_minutes(@hour, @min)
end
time() click to toggle source

Returns a Time object with the correct hour and min values. The date is 10th June 1963.

@return [DateTime] The time using the date of 10th June 1963 (My Birthday)

# File lib/workpattern/clock.rb, line 64
def time
  DateTime.new(1963, 6, 10, hour, min)
end
to_s() click to toggle source

@return [String] representation of Clock value as 'hh:mn minutes'

# File lib/workpattern/clock.rb, line 69
def to_s
  hour.to_s.concat(':').concat(min.to_s).concat(' ').concat(minutes.to_s)
end

Private Instance Methods

total_minutes(hours, mins) click to toggle source
# File lib/workpattern/clock.rb, line 75
def total_minutes(hours, mins)
  (hours * 60) + mins
end