class ClockTime
Attributes
hour[R]
minute[R]
Public Class Methods
new(hour, min)
click to toggle source
# File lib/clock_time.rb, line 19 def initialize(hour, min) @hour = hour @minute = min if !valid_hour? || !valid_minute? raise ArgumentError, 'invalid clock time' end end
parse(string)
click to toggle source
# File lib/clock_time.rb, line 10 def parse(string) if string =~ /\A(\d{2})[^\d]?(\d{2})\z/ new($1.to_i, $2.to_i) else raise ArgumentError, 'invalid clock time' end end
Public Instance Methods
+(augend)
click to toggle source
# File lib/clock_time.rb, line 56 def +(augend) if augend.is_a?(self.class) augend = augend.to_duration end to_duration + augend end
-(minuend)
click to toggle source
# File lib/clock_time.rb, line 63 def -(minuend) if minuend.is_a?(self.class) minuend = minuend.to_duration end to_duration - minuend end
<=>(clock_time)
click to toggle source
# File lib/clock_time.rb, line 52 def <=>(clock_time) to_duration <=> clock_time.to_duration end
next_time(base_time)
click to toggle source
# File lib/clock_time.rb, line 36 def next_time(base_time) t = replace_time(base_time) return t + 1.day if t < base_time t end
prev_time(base_time)
click to toggle source
# File lib/clock_time.rb, line 42 def prev_time(base_time) t = replace_time(base_time) return t - 1.day if t > base_time t end
replace_time(time)
click to toggle source
# File lib/clock_time.rb, line 48 def replace_time(time) time.change(hour: @hour, min: @minute) end
to_duration()
click to toggle source
# File lib/clock_time.rb, line 27 def to_duration @hour.hours + @minute.minutes end
to_time(date)
click to toggle source
# File lib/clock_time.rb, line 31 def to_time(date) midnight = date.to_time replace_time(midnight) end
valid_hour?()
click to toggle source
# File lib/clock_time.rb, line 70 def valid_hour? return false unless @hour.is_a?(Integer) return true if @hour >= 0 && @hour <= 24 false end
valid_minute?()
click to toggle source
# File lib/clock_time.rb, line 76 def valid_minute? return false unless @minute.is_a?(Integer) return true if @minute >= 0 && @minute <= 60 false end