distance 0.3.1 → 1.0.0
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.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/README.md +24 -12
- data/lib/distance.rb +33 -10
- data/lib/distance/core_ext/numeric.rb +18 -0
- data/lib/distance/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b121430d288441c455ca6d8dc8b56f02892243a
|
4
|
+
data.tar.gz: 92a6e9bd2dd1c761ed4bdc5b3217195b04fc4119
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13215885766b11f63db7a002b415c85000791db617006498de4d343262effe524d2d9b54b96289438c65e70fa59a696ab3d192acf038f9f895e829f5c8813867
|
7
|
+
data.tar.gz: 36a23b5ffefa619a8a04af53e015098e5995735466bb2835c483028268dec9804e8154e12e5fd540e5f83d9d46b5a2b8ea9750707ab8487a9c9e74f1ffa7887d
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -26,7 +26,7 @@ There are multiple options for constructing a new `Distance` instance. The basic
|
|
26
26
|
constructor expects an `Float` argument that is the distannce is meters, e.g.
|
27
27
|
|
28
28
|
```ruby
|
29
|
-
eight_hundred_meters = Distance.
|
29
|
+
eight_hundred_meters = Distance.meters(800)
|
30
30
|
```
|
31
31
|
|
32
32
|
If you are working with different units, you can construct a new instance with the
|
@@ -46,19 +46,31 @@ five_km.to_miles
|
|
46
46
|
# => 3.11
|
47
47
|
```
|
48
48
|
|
49
|
+
### Supported units
|
50
|
+
|
51
|
+
* inches
|
52
|
+
* feet
|
53
|
+
* meters
|
54
|
+
* kilometers
|
55
|
+
* miles
|
56
|
+
|
57
|
+
Additionally, the following constants are available
|
58
|
+
|
59
|
+
* `Distance::MARATHON`
|
60
|
+
|
49
61
|
### Distance math
|
50
62
|
|
51
63
|
You can perform basic math with distances
|
52
64
|
|
53
65
|
```ruby
|
54
|
-
Distance.
|
55
|
-
# => Distance.
|
56
|
-
Distance.
|
57
|
-
# => Distance.
|
58
|
-
Distance.
|
59
|
-
# => Distance.
|
60
|
-
Distance.
|
61
|
-
# => Distance.
|
66
|
+
Distance.meters(1) + Distance.meters(2)
|
67
|
+
# => Distance.meters(3)
|
68
|
+
Distance.meters(3) - Distance.meters(1)
|
69
|
+
# => Distance.meters(2)
|
70
|
+
Distance.meters(3) * 2
|
71
|
+
# => Distance.meters(6)
|
72
|
+
Distance.meters(6) / 2
|
73
|
+
# => Distance.meters(3)
|
62
74
|
```
|
63
75
|
|
64
76
|
### Distance equality
|
@@ -66,7 +78,7 @@ Distance.new(6) / 2
|
|
66
78
|
You can also compare distances
|
67
79
|
|
68
80
|
```ruby
|
69
|
-
Distance.
|
81
|
+
Distance.meters(10) > Distance.meters(9)
|
70
82
|
# => true
|
71
83
|
Distance.miles(10) <= Distance.kilometers(10)
|
72
84
|
# => false
|
@@ -87,8 +99,8 @@ This adds the following convenience methods to `Float` and `Integer`
|
|
87
99
|
# => Distance.miles(26.2)
|
88
100
|
1.kilometer
|
89
101
|
# => Distance.kilometers(1)
|
90
|
-
5 * Distance.
|
91
|
-
# => Distance.
|
102
|
+
5 * Distance.meters(100)
|
103
|
+
# => Distance.meters(500)
|
92
104
|
```
|
93
105
|
|
94
106
|
## Development
|
data/lib/distance.rb
CHANGED
@@ -4,6 +4,8 @@ class Distance
|
|
4
4
|
# Multipliers from standard race units to meters
|
5
5
|
module Multiplier
|
6
6
|
KILOMETER = 1000.0
|
7
|
+
INCH = 0.0254
|
8
|
+
FOOT = 0.3048
|
7
9
|
MILE = 1609.344
|
8
10
|
end
|
9
11
|
private_constant :Multiplier
|
@@ -12,13 +14,26 @@ class Distance
|
|
12
14
|
@distance_in_meters = distance_in_meters.to_f
|
13
15
|
end
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
+
private_class_method :new
|
18
|
+
|
19
|
+
MARATHON = new(42195.0)
|
20
|
+
|
21
|
+
def self.meters(n)
|
22
|
+
new(n)
|
23
|
+
end
|
17
24
|
|
18
25
|
def self.kilometers(n)
|
19
26
|
new(n * Multiplier::KILOMETER)
|
20
27
|
end
|
21
28
|
|
29
|
+
def self.inches(n)
|
30
|
+
new(n * Multiplier::INCH)
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.feet(n)
|
34
|
+
new(n * Multiplier::FOOT)
|
35
|
+
end
|
36
|
+
|
22
37
|
def self.miles(n)
|
23
38
|
new(n * Multiplier::MILE)
|
24
39
|
end
|
@@ -27,40 +42,48 @@ class Distance
|
|
27
42
|
distance_in_meters
|
28
43
|
end
|
29
44
|
|
30
|
-
def to_miles
|
31
|
-
(distance_in_meters / Multiplier::MILE).round(2)
|
32
|
-
end
|
33
|
-
|
34
45
|
def to_kilometers
|
35
46
|
distance_in_meters / Multiplier::KILOMETER
|
36
47
|
end
|
37
48
|
|
49
|
+
def to_inches
|
50
|
+
(distance_in_meters / Multiplier::INCH)
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_feet
|
54
|
+
(distance_in_meters / Multiplier::FOOT)
|
55
|
+
end
|
56
|
+
|
57
|
+
def to_miles
|
58
|
+
(distance_in_meters / Multiplier::MILE)
|
59
|
+
end
|
60
|
+
|
38
61
|
def +(other)
|
39
62
|
unless other.is_a?(Distance)
|
40
63
|
raise ArgumentError, 'Can only add a Distance to a Distance'
|
41
64
|
end
|
42
|
-
Distance.
|
65
|
+
Distance.meters(to_f + other.to_f)
|
43
66
|
end
|
44
67
|
|
45
68
|
def -(other)
|
46
69
|
unless other.is_a?(Distance)
|
47
70
|
raise ArgumentError, 'Can only subtract a Distance from a Distance'
|
48
71
|
end
|
49
|
-
Distance.
|
72
|
+
Distance.meters(to_f - other.to_f)
|
50
73
|
end
|
51
74
|
|
52
75
|
def *(multiplier)
|
53
76
|
unless multiplier.is_a?(Numeric)
|
54
77
|
raise ArgumentError, 'Can only multiply a Distance with a number'
|
55
78
|
end
|
56
|
-
Distance.
|
79
|
+
Distance.meters(to_f * multiplier)
|
57
80
|
end
|
58
81
|
|
59
82
|
def /(divisor)
|
60
83
|
unless divisor.is_a?(Numeric)
|
61
84
|
raise ArgumentError, 'Can only divide a Distance by a number'
|
62
85
|
end
|
63
|
-
Distance.
|
86
|
+
Distance.meters(to_f / divisor)
|
64
87
|
end
|
65
88
|
|
66
89
|
def >(other)
|
@@ -1,10 +1,28 @@
|
|
1
1
|
class Numeric
|
2
|
+
def meters
|
3
|
+
Distance.meters(self)
|
4
|
+
end
|
5
|
+
|
6
|
+
alias_method :meter, :meters
|
7
|
+
|
2
8
|
def kilometers
|
3
9
|
Distance.kilometers(self)
|
4
10
|
end
|
5
11
|
|
6
12
|
alias_method :kilometer, :kilometers
|
7
13
|
|
14
|
+
def inches
|
15
|
+
Distance.inches(self)
|
16
|
+
end
|
17
|
+
|
18
|
+
alias_method :inch, :inches
|
19
|
+
|
20
|
+
def feet
|
21
|
+
Distance.feet(self)
|
22
|
+
end
|
23
|
+
|
24
|
+
alias_method :foot, :feet
|
25
|
+
|
8
26
|
def miles
|
9
27
|
Distance.miles(self)
|
10
28
|
end
|
data/lib/distance/version.rb
CHANGED