triathlon 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/bike.rb +27 -18
- data/lib/triathlon.rb +0 -4
- data/test/unit/test_bike.rb +11 -33
- metadata +4 -4
data/lib/bike.rb
CHANGED
@@ -1,27 +1,36 @@
|
|
1
1
|
require 'converter'
|
2
2
|
|
3
|
+
class TimePresenter
|
4
|
+
def self.present_hours_as_string(time)
|
5
|
+
hours = time.floor
|
6
|
+
fraction = time - hours
|
7
|
+
minutes = (60.0 * fraction)
|
8
|
+
minutes_string = minutes.floor.to_s
|
9
|
+
minutes_string = "0#{minutes_string}" if minutes < 10.0
|
10
|
+
fraction = minutes - minutes.floor
|
11
|
+
seconds = (60.0 * fraction)
|
12
|
+
seconds_string = seconds.round.to_s
|
13
|
+
seconds_string = "0#{seconds_string}" if seconds < 10.0
|
14
|
+
"#{hours}:#{minutes_string}:#{seconds_string}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
3
18
|
class Bike
|
4
|
-
|
5
|
-
|
19
|
+
attr_accessor :distance, :speed
|
20
|
+
|
21
|
+
def initialize(distance, speed)
|
22
|
+
self.distance = distance.to_f
|
23
|
+
self.speed = speed.to_f
|
6
24
|
end
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
options_hash = options[0]
|
12
|
-
if options_hash[:time_unit] == :hours
|
13
|
-
time_in_seconds = @converter.hours_to_seconds time
|
14
|
-
elsif options_hash[:time_unit] == :minutes
|
15
|
-
time_in_seconds = @converter.minutes_to_seconds time
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
speed_in_miles_per_second = distance / time_in_seconds
|
20
|
-
@converter.mps_to_mph speed_in_miles_per_second
|
25
|
+
|
26
|
+
def compute_time
|
27
|
+
time = distance / speed # time in hours
|
28
|
+
TimePresenter.present_hours_as_string(time)
|
21
29
|
end
|
22
30
|
|
23
|
-
def
|
24
|
-
distance
|
31
|
+
def to_s
|
32
|
+
p distance
|
33
|
+
p speed
|
25
34
|
end
|
26
35
|
end
|
27
36
|
|
data/lib/triathlon.rb
CHANGED
data/test/unit/test_bike.rb
CHANGED
@@ -7,40 +7,18 @@ require 'test_helper'
|
|
7
7
|
require 'bike'
|
8
8
|
|
9
9
|
class TestBike < Test::Unit::TestCase
|
10
|
-
should "compute bike speed" do
|
11
|
-
distance = 14.5
|
12
|
-
time = 2400 # default is time in seconds
|
13
|
-
bikeRace = Bike.new
|
14
|
-
mph = bikeRace.compute_speed distance, time
|
15
|
-
expected_mph = 21.75
|
16
|
-
assert_in_delta(expected_mph, mph, 0.001)
|
17
|
-
end
|
18
|
-
|
19
|
-
should "compute ride time given speed and distance" do
|
10
|
+
should "compute bike time from distance and speed" do
|
20
11
|
distance = 24.8
|
21
12
|
speed = 23.0
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
bikeRace = Bike.new
|
33
|
-
mph = bikeRace.compute_speed(distance, time, :time_unit => :hours)
|
34
|
-
expected_mph = 19.84
|
35
|
-
assert_in_delta(expected_mph, mph, 0.001)
|
36
|
-
end
|
37
|
-
|
38
|
-
should "compute mph given time in minutes" do
|
39
|
-
distance = 14.5
|
40
|
-
time = 41.43
|
41
|
-
bikeRace = Bike.new
|
42
|
-
mph = bikeRace.compute_speed(distance, time, :time_unit => :minutes)
|
43
|
-
expected_mph = 21.0
|
44
|
-
assert_in_delta(expected_mph, mph, 0.001)
|
13
|
+
bike = Bike.new(distance, speed)
|
14
|
+
assert_in_delta(distance, bike.distance, 0.001)
|
15
|
+
assert_in_delta(speed, bike.speed, 0.001)
|
16
|
+
|
17
|
+
puts bike
|
18
|
+
|
19
|
+
time = bike.compute_time
|
20
|
+
expected_time = "1:04:42"
|
21
|
+
p time
|
22
|
+
assert_equal(expected_time, time)
|
45
23
|
end
|
46
24
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: triathlon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 7
|
10
|
+
version: 0.0.7
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Waylon Calabrese
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-08
|
18
|
+
date: 2010-09-08 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|