Class: Clock

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/a_clockwork_ruby/clock.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hour = nil, min = nil, sec = nil, usec = nil) ⇒ Clock

Returns a new instance of Clock.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/a_clockwork_ruby/clock.rb', line 5

def initialize(hour = nil, min = nil, sec = nil, usec = nil)
  if hour.nil?
    time = Time.now
    @hour = time.hour
    @min = time.min
    @sec = time.sec
    @usec = time.usec
  else
    @hour = hour
    @min = min || 0
    @sec = sec || 0
    @usec = usec || 0
  end
end

Instance Attribute Details

#hourObject

Returns the value of attribute hour.



3
4
5
# File 'lib/a_clockwork_ruby/clock.rb', line 3

def hour
  @hour
end

#minObject

Returns the value of attribute min.



3
4
5
# File 'lib/a_clockwork_ruby/clock.rb', line 3

def min
  @min
end

#secObject

Returns the value of attribute sec.



3
4
5
# File 'lib/a_clockwork_ruby/clock.rb', line 3

def sec
  @sec
end

#usecObject

Returns the value of attribute usec.



3
4
5
# File 'lib/a_clockwork_ruby/clock.rb', line 3

def usec
  @usec
end

Class Method Details

.from_i(seconds) ⇒ Object



24
25
26
# File 'lib/a_clockwork_ruby/clock.rb', line 24

def self.from_i seconds
  self.new(seconds / 60**2 % 60**2, seconds / 60 % 60, seconds % 60)
end

.nowObject



20
21
22
# File 'lib/a_clockwork_ruby/clock.rb', line 20

def self.now
  self.new
end

Instance Method Details

#<=>(other) ⇒ Object



73
74
75
76
# File 'lib/a_clockwork_ruby/clock.rb', line 73

def <=> other
  time = Time.now
  to_time_helper(time) <=> other.send(:to_time_helper, time)
end

#==(other) ⇒ Object



68
69
70
71
# File 'lib/a_clockwork_ruby/clock.rb', line 68

def == other
  time = Time.now
  to_time_helper(time) == other.send(:to_time_helper, time)
end

#inspectObject



60
61
62
# File 'lib/a_clockwork_ruby/clock.rb', line 60

def inspect
  strfclock("%l:%M %p")
end

#strfclock(format) ⇒ Object



48
49
50
# File 'lib/a_clockwork_ruby/clock.rb', line 48

def strfclock format
  to_time.strftime(format)
end

#to_iObject



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

def to_i
  @sec + (@min * 60) + (@hour * 60**2)
end

#to_sObject



56
57
58
# File 'lib/a_clockwork_ruby/clock.rb', line 56

def to_s
  strfclock("%l:%M %p")
end

#to_timeObject



52
53
54
# File 'lib/a_clockwork_ruby/clock.rb', line 52

def to_time
  to_time_helper(Time.now)
end