distance 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/README.md +49 -1
- data/lib/distance/fixnum_extensions.rb +1 -0
- data/lib/distance/version.rb +1 -1
- data/lib/distance.rb +49 -5
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 687cc209435ce9c3846923b3e45bbcf93a98657b
|
4
|
+
data.tar.gz: 420b0b31cb846bf09e18d6cdf5a2d450351ac5de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5330a178f261c0f5e1e353297d236b99179552d68e2d567bc54c1a41657b7f88bc7ba470ac32bebec37f3ed37e7ac4e98c6b4722f679b60b676a1e0741b6877b
|
7
|
+
data.tar.gz: 4429e65174098ffd8e237cdb9b27d8a9bdf53baab47ac1ea94f0e0686cc9da3e157d420a00625e6108152d13d9d035c62885f249d8f6134b128cd69eea33165f
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -20,7 +20,55 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
23
|
+
There are multiple options for constructing a new `Distance` instance. The basic
|
24
|
+
constructor expects an `Float` argument that is the distannce is meters, e.g.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
eight_hundred_meters = Distance.new(800)
|
28
|
+
```
|
29
|
+
|
30
|
+
If you are working with different units, you can construct a new instance with the
|
31
|
+
following convenience class methods
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
ten_miles = Distance.miles(10)
|
35
|
+
five_km = Distance.kilometers(5)
|
36
|
+
```
|
37
|
+
|
38
|
+
Once you have a `Distance` instance, you can convert it to another base unit like
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
ten_miles.to_kilometers
|
42
|
+
# => 16.09344
|
43
|
+
five_km.to_miles
|
44
|
+
# => 3.11
|
45
|
+
```
|
46
|
+
|
47
|
+
### Distance math
|
48
|
+
|
49
|
+
You can perform basic math with distances
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
Distance.new(1) + Distance.new(2)
|
53
|
+
# => Distance.new(3)
|
54
|
+
Distance.new(3) - Distance.new(1)
|
55
|
+
# => Distance.new(2)
|
56
|
+
Distance.new(3) * 2
|
57
|
+
# => Distance.new(6)
|
58
|
+
Distance.new(6) / 2
|
59
|
+
# => Distance.new(3)
|
60
|
+
```
|
61
|
+
|
62
|
+
### Distance equality
|
63
|
+
|
64
|
+
You can also compare distances
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
Distance.new(10) > Distance.new(9)
|
68
|
+
# => true
|
69
|
+
Distance.miles(10) <= Distance.kilometers(10)
|
70
|
+
# => false
|
71
|
+
```
|
24
72
|
|
25
73
|
## Development
|
26
74
|
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative '../core_ext/numeric'
|
data/lib/distance/version.rb
CHANGED
data/lib/distance.rb
CHANGED
@@ -23,10 +23,6 @@ class Distance
|
|
23
23
|
new(n * Multiplier::MILE)
|
24
24
|
end
|
25
25
|
|
26
|
-
def in_kilometers?
|
27
|
-
distance_in_meters % Multiplier::KILOMETER == 0
|
28
|
-
end
|
29
|
-
|
30
26
|
def to_f
|
31
27
|
distance_in_meters
|
32
28
|
end
|
@@ -39,9 +35,57 @@ class Distance
|
|
39
35
|
distance_in_meters / Multiplier::KILOMETER
|
40
36
|
end
|
41
37
|
|
38
|
+
def +(other)
|
39
|
+
unless other.is_a?(Distance)
|
40
|
+
raise ArgumentError, 'Can only add a Distance to a Distance'
|
41
|
+
end
|
42
|
+
Distance.new(to_f + other.to_f)
|
43
|
+
end
|
44
|
+
|
45
|
+
def -(other)
|
46
|
+
unless other.is_a?(Distance)
|
47
|
+
raise ArgumentError, 'Can only subtract a Distance from a Distance'
|
48
|
+
end
|
49
|
+
Distance.new(to_f - other.to_f)
|
50
|
+
end
|
51
|
+
|
52
|
+
def *(multiplier)
|
53
|
+
unless multiplier.is_a?(Numeric)
|
54
|
+
raise ArgumentError, 'Can only multiply a Distance with a number'
|
55
|
+
end
|
56
|
+
Distance.new(to_f * multiplier)
|
57
|
+
end
|
58
|
+
|
59
|
+
def /(divisor)
|
60
|
+
unless divisor.is_a?(Numeric)
|
61
|
+
raise ArgumentError, 'Can only divide a Distance by a number'
|
62
|
+
end
|
63
|
+
Distance.new(to_f / divisor)
|
64
|
+
end
|
65
|
+
|
66
|
+
def >(other)
|
67
|
+
return false unless other.is_a?(Distance)
|
68
|
+
to_f > other.to_f
|
69
|
+
end
|
70
|
+
|
71
|
+
def >=(other)
|
72
|
+
return false unless other.is_a?(Distance)
|
73
|
+
to_f >= other.to_f
|
74
|
+
end
|
75
|
+
|
42
76
|
def ==(other)
|
43
77
|
return false unless other.is_a?(Distance)
|
44
|
-
|
78
|
+
to_f == other.to_f
|
79
|
+
end
|
80
|
+
|
81
|
+
def <(other)
|
82
|
+
return false unless other.is_a?(Distance)
|
83
|
+
to_f < other.to_f
|
84
|
+
end
|
85
|
+
|
86
|
+
def <=(other)
|
87
|
+
return false unless other.is_a?(Distance)
|
88
|
+
to_f <= other.to_f
|
45
89
|
end
|
46
90
|
|
47
91
|
private
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: distance
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Collier
|
@@ -85,6 +85,7 @@ files:
|
|
85
85
|
- bin/setup
|
86
86
|
- distance.gemspec
|
87
87
|
- lib/distance.rb
|
88
|
+
- lib/distance/fixnum_extensions.rb
|
88
89
|
- lib/distance/version.rb
|
89
90
|
homepage: https://github.com/tcollier/distance
|
90
91
|
licenses:
|