m9t 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/m9t.rb +1 -1
- data/lib/m9t/speed.rb +22 -6
- data/test/speed_test.rb +18 -4
- metadata +2 -2
data/lib/m9t.rb
CHANGED
data/lib/m9t/speed.rb
CHANGED
@@ -14,7 +14,8 @@ module M9t
|
|
14
14
|
SECONDS_PER_HOUR = 60.0 * 60
|
15
15
|
MS_TO_KMH = SECONDS_PER_HOUR / M9t::Distance::METERS_PER_KILOMETER
|
16
16
|
MS_TO_MPH = SECONDS_PER_HOUR / M9t::Distance::METERS_PER_MILE
|
17
|
-
|
17
|
+
KNOTS_TO_KMH = 1.852
|
18
|
+
MS_TO_KNOTS = 1.0 / (KNOTS_TO_KMH / SECONDS_PER_HOUR * M9t::Distance::METERS_PER_KILOMETER)
|
18
19
|
|
19
20
|
include M9t::Base
|
20
21
|
|
@@ -22,12 +23,17 @@ module M9t
|
|
22
23
|
|
23
24
|
# Accepts kilometers per hour and returns a M9t::Speed instance
|
24
25
|
def kilometers_per_hour(kmh, options = {})
|
25
|
-
new(kmh.to_f
|
26
|
+
new(kmh.to_f / MS_TO_KMH, options)
|
26
27
|
end
|
27
28
|
|
28
29
|
# Accepts miles per hour and returns a M9t::Speed instance
|
29
30
|
def miles_per_hour(mph, options = {})
|
30
|
-
new(mph.to_f
|
31
|
+
new(mph.to_f / MS_TO_MPH, options)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Accepts knots and returns a M9t::Speed instance
|
35
|
+
def knots(knots, options = {})
|
36
|
+
new(knots.to_f / MS_TO_KNOTS, options)
|
31
37
|
end
|
32
38
|
|
33
39
|
# Identity conversion. Simply returns the supplied number
|
@@ -37,17 +43,27 @@ module M9t
|
|
37
43
|
|
38
44
|
# Converts meters per second to kilometers per hour
|
39
45
|
def to_kilometers_per_hour(mps)
|
40
|
-
mps.to_f
|
46
|
+
mps.to_f * MS_TO_KMH
|
41
47
|
end
|
42
48
|
|
43
49
|
# Converts meters per second to miles per hour
|
44
50
|
def to_miles_per_hour(mps)
|
45
|
-
mps.to_f
|
51
|
+
mps.to_f * MS_TO_MPH
|
46
52
|
end
|
47
53
|
|
48
54
|
# Converts meters per second to knots
|
49
55
|
def to_knots(mps)
|
50
|
-
mps.to_f
|
56
|
+
mps.to_f * MS_TO_KNOTS
|
57
|
+
end
|
58
|
+
|
59
|
+
# Converts knots to kilometers hour
|
60
|
+
def knots_to_kilometers_per_hour(knots)
|
61
|
+
to_kilometers_per_hour(knots.to_f / MS_TO_KNOTS)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Converts knots to kilometers hour
|
65
|
+
def kilometers_per_hour_to_knots(knh)
|
66
|
+
to_knots(kmh.to_f / MS_TO_KMH)
|
51
67
|
end
|
52
68
|
|
53
69
|
end
|
data/test/speed_test.rb
CHANGED
@@ -25,16 +25,30 @@ class TestM9tSpeed < Test::Unit::TestCase
|
|
25
25
|
assert_equal('speed', M9t::Speed.measurement_name)
|
26
26
|
end
|
27
27
|
|
28
|
+
# conversion constants
|
29
|
+
|
30
|
+
def test_knot_conversion
|
31
|
+
assert_in_delta(1.9438, M9t::Speed::MS_TO_KNOTS, 0.0001)
|
32
|
+
end
|
33
|
+
|
28
34
|
# input conversions
|
29
35
|
|
36
|
+
def test_class_kilometers_per_hour
|
37
|
+
assert_in_delta(0.2778, M9t::Speed.kilometers_per_hour(1).value, 0.0001)
|
38
|
+
end
|
39
|
+
|
30
40
|
def test_class_miles_per_hour
|
31
|
-
|
41
|
+
assert_in_delta(0.447, M9t::Speed.miles_per_hour(1).value, 0.0001)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_class_knots
|
45
|
+
assert_in_delta 0.5144, M9t::Speed.knots(1).value, 0.0001
|
32
46
|
end
|
33
47
|
|
34
48
|
# output conversions
|
35
49
|
|
36
50
|
def test_class_to_miles_per_hour
|
37
|
-
|
51
|
+
assert_in_delta(100.6621, M9t::Speed.to_miles_per_hour(45), 0.0001)
|
38
52
|
end
|
39
53
|
|
40
54
|
# Instance methods
|
@@ -48,11 +62,11 @@ class TestM9tSpeed < Test::Unit::TestCase
|
|
48
62
|
# output conversions
|
49
63
|
|
50
64
|
def test_kmh
|
51
|
-
assert_equal(
|
65
|
+
assert_equal(162, M9t::Speed.new(45).to_kilometers_per_hour)
|
52
66
|
end
|
53
67
|
|
54
68
|
def test_mph
|
55
|
-
|
69
|
+
assert_in_delta(100.6621, M9t::Speed.new(45).to_miles_per_hour, 0.0001)
|
56
70
|
end
|
57
71
|
|
58
72
|
# to_s
|