distance 0.3.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0458b08c5bfa15588f29a9d14de9514610241e03
4
- data.tar.gz: cf0704ea44beea40f4f21550f210815b44a22d0d
3
+ metadata.gz: 8b121430d288441c455ca6d8dc8b56f02892243a
4
+ data.tar.gz: 92a6e9bd2dd1c761ed4bdc5b3217195b04fc4119
5
5
  SHA512:
6
- metadata.gz: cb694566926dbc4f0983c51af4af03cafcf94a64847fb17f307376dc928fff0c6d765b26024fda5da8cd0541f51d25e0745ca87a4ca1296a3536a9462960ec2d
7
- data.tar.gz: 4ff910bf125cfca7643cacbc5e98dedb09ee797588a5b6be71b048212ca7a9601a808de75e23dbbfddcd49d8f4e63434396f9d295b1009bf3e746930b3f3e97a
6
+ metadata.gz: 13215885766b11f63db7a002b415c85000791db617006498de4d343262effe524d2d9b54b96289438c65e70fa59a696ab3d192acf038f9f895e829f5c8813867
7
+ data.tar.gz: 36a23b5ffefa619a8a04af53e015098e5995735466bb2835c483028268dec9804e8154e12e5fd540e5f83d9d46b5a2b8ea9750707ab8487a9c9e74f1ffa7887d
@@ -1,7 +1,7 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.3.7
4
+ - 2.2.7
5
5
  - 2.3.4
6
6
  - 2.4.1
7
7
  before_install: gem install bundler -v 1.14.6
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.new(800)
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.new(1) + Distance.new(2)
55
- # => Distance.new(3)
56
- Distance.new(3) - Distance.new(1)
57
- # => Distance.new(2)
58
- Distance.new(3) * 2
59
- # => Distance.new(6)
60
- Distance.new(6) / 2
61
- # => Distance.new(3)
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.new(10) > Distance.new(9)
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.new(1000)
91
- # => Distance.new(5000)
102
+ 5 * Distance.meters(100)
103
+ # => Distance.meters(500)
92
104
  ```
93
105
 
94
106
  ## Development
@@ -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
- MARATHON = new(42195.0)
16
- HALF_MARATHON = new(21097.5)
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.new(to_f + other.to_f)
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.new(to_f - other.to_f)
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.new(to_f * multiplier)
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.new(to_f / divisor)
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
@@ -1,3 +1,3 @@
1
1
  class Distance
2
- VERSION = '0.3.1'
2
+ VERSION = '1.0.0'
3
3
  end
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.3.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Collier