Class: Clock
- Inherits:
-
Object
- Object
- Clock
- Includes:
- Comparable
- Defined in:
- lib/a_clockwork_ruby/clock.rb
Instance Attribute Summary collapse
-
#hour ⇒ Object
Returns the value of attribute hour.
-
#min ⇒ Object
Returns the value of attribute min.
-
#sec ⇒ Object
Returns the value of attribute sec.
-
#usec ⇒ Object
Returns the value of attribute usec.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
-
#initialize(hour = nil, min = nil, sec = nil, usec = nil) ⇒ Clock
constructor
A new instance of Clock.
- #inspect ⇒ Object
- #strfclock(format) ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
- #to_time ⇒ Object
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
#hour ⇒ Object
Returns the value of attribute hour.
3 4 5 |
# File 'lib/a_clockwork_ruby/clock.rb', line 3 def hour @hour end |
#min ⇒ Object
Returns the value of attribute min.
3 4 5 |
# File 'lib/a_clockwork_ruby/clock.rb', line 3 def min @min end |
#sec ⇒ Object
Returns the value of attribute sec.
3 4 5 |
# File 'lib/a_clockwork_ruby/clock.rb', line 3 def sec @sec end |
#usec ⇒ Object
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 |
.now ⇒ Object
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 |
#inspect ⇒ Object
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_i ⇒ Object
64 65 66 |
# File 'lib/a_clockwork_ruby/clock.rb', line 64 def to_i @sec + (@min * 60) + (@hour * 60**2) end |
#to_s ⇒ Object
56 57 58 |
# File 'lib/a_clockwork_ruby/clock.rb', line 56 def to_s strfclock("%l:%M %p") end |
#to_time ⇒ Object
52 53 54 |
# File 'lib/a_clockwork_ruby/clock.rb', line 52 def to_time to_time_helper(Time.now) end |