Class: Workpattern::Clock

Inherits:
Object
  • Object
show all
Defined in:
lib/workpattern/clock.rb

Overview

Represents time on a clock in hours and minutes.

Examples:

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

Instance Method Summary collapse

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.

Parameters:

  • hour (Integer) (defaults to: 0)

    number of hours

  • min (Integer) (defaults to: 0)

    number of minutes

Since:

  • 0.2.0



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

#hourInteger

Returns the hour of the clock (0-23)

Returns:

  • (Integer)

    hour of Clock from 0 to 23.

Since:

  • 0.2.0



47
48
49
# File 'lib/workpattern/clock.rb', line 47

def hour
  @hour % 24
end

#minInteger

Returns the minute of the clock (0-59)

Returns:

  • (Integer)

    minute of Clock from 0 to 59

Since:

  • 0.2.0



55
56
57
# File 'lib/workpattern/clock.rb', line 55

def min
  @min % 60
end

#minutesInteger

Returns the total number of minutes

Returns:

  • (Integer)

    total minutes represented by the Clock object

Since:

  • 0.2.0



39
40
41
# File 'lib/workpattern/clock.rb', line 39

def minutes
  total_minutes(@hour, @min)
end

#timeDateTime

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

Returns:

  • (DateTime)

    The time using the date of 10th June 1963 (My Birthday)

Since:

  • 0.2.0



64
65
66
# File 'lib/workpattern/clock.rb', line 64

def time
  DateTime.new(1963, 6, 10, hour, min)
end

#to_sString

Returns representation of Clock value as ‘hh:mn minutes’.

Returns:

  • (String)

    representation of Clock value as ‘hh:mn minutes’

Since:

  • 0.2.0



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