forked from alexch/learn_ruby
-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathtimer.rb
More file actions
46 lines (36 loc) · 868 Bytes
/
timer.rb
File metadata and controls
46 lines (36 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# what we're trying to do here is create an object "Timer" with a seconds method that takes the number of seconds elapsed, and a time_string method that returns in hour: minute: second format
require 'pry'
class Timer
def initialize(s = 0, m = 0, h = 0)
@seconds = s
@minutes = m
@hours = h
end
def seconds
@seconds
end
def seconds=(number)
@seconds = number
end
def time_string
calculate_clock_time
make_time_read_friendly
end
end
def calculate_clock_time
if @seconds > 60
@minutes = @seconds / 60
@seconds -= @minutes * 60
end
if @minutes > 60
@hours = @minutes / 60
@minutes -= @hours * 60
end
end
def make_time_read_friendly
time_array = [@hours.to_s,@minutes.to_s,@seconds.to_s]
time_array.map do |time|
time.prepend('0') until time.length == 2
end
time_array.join(':')
end