ruby-measurement 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. data/.travis.yml +4 -0
  2. data/CHANGELOG.md +21 -0
  3. data/README.md +2 -0
  4. data/Rakefile +3 -0
  5. data/lib/ruby-measurement/definitions/metric.rb +1 -0
  6. data/lib/ruby-measurement/definitions/metric/capacity.rb +12 -82
  7. data/lib/ruby-measurement/definitions/metric/volume.rb +82 -12
  8. data/lib/ruby-measurement/definitions/us_customary.rb +1 -0
  9. data/lib/ruby-measurement/definitions/us_customary/capacity.rb +29 -0
  10. data/lib/ruby-measurement/definitions/us_customary/length.rb +0 -20
  11. data/lib/ruby-measurement/definitions/us_customary/weight.rb +6 -6
  12. data/lib/ruby-measurement/measurement.rb +5 -14
  13. data/lib/ruby-measurement/unit.rb +52 -11
  14. data/lib/ruby-measurement/version.rb +1 -1
  15. data/ruby-measurement.gemspec +1 -0
  16. data/spec/ruby-measurement/definitions/metric/area_spec.rb +69 -0
  17. data/spec/ruby-measurement/definitions/metric/capacity_spec.rb +41 -0
  18. data/spec/ruby-measurement/definitions/metric/length_spec.rb +261 -0
  19. data/spec/ruby-measurement/definitions/metric/volume_spec.rb +261 -0
  20. data/spec/ruby-measurement/definitions/metric/weight_spec.rb +325 -0
  21. data/spec/ruby-measurement/definitions/us_customary/area_spec.rb +149 -0
  22. data/spec/ruby-measurement/definitions/us_customary/capacity_spec.rb +69 -0
  23. data/spec/ruby-measurement/definitions/us_customary/length_spec.rb +261 -0
  24. data/spec/ruby-measurement/definitions/us_customary/volume_spec.rb +201 -0
  25. data/spec/ruby-measurement/definitions/us_customary/weight_spec.rb +149 -0
  26. data/spec/ruby-measurement/measurement_spec.rb +342 -0
  27. data/spec/ruby-measurement/unit_builder_spec.rb +102 -0
  28. data/spec/ruby-measurement/unit_spec.rb +120 -0
  29. data/spec/spec_helper.rb +5 -0
  30. data/tasks/rspec.rake +6 -0
  31. metadata +48 -5
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - ruby-head
data/CHANGELOG.md ADDED
@@ -0,0 +1,21 @@
1
+ ## ruby-measurement 1.1.0 (Feb 6, 2013)
2
+
3
+ * Move unit definition logic into `Unit` class. *Matt Huggins*
4
+ * Create test suite. *Matt Huggins*
5
+ * Fix parsing of `'` and `"` for feet and inches. *Matt Huggins*
6
+ * Implement missing metric weights and U.S. customary capacities.
7
+ *Matt Huggins*
8
+ * Fix several conversions that were found to be miscalculating.
9
+ *Matt Huggins*
10
+
11
+ ## ruby-measurement 1.0.0 (Jan 22, 2013)
12
+
13
+ * Fix parsing of mixed fractions. *Matt Huggins*
14
+ * Allow parsing of quantity & unit to work when not separated by a space.
15
+ *Matt Huggins*
16
+ * Change `Measurement` initializer to accept quantity & unit instead of a
17
+ single string for parsing. *Matt Huggins*
18
+
19
+ ## ruby-measurement 0.0.1 (Jan 21, 2013)
20
+
21
+ * Initial release. *Matt Huggins*
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Measurement
2
2
 
3
+ [![Build Status](https://secure.travis-ci.org/mhuggins/ruby-measurement.png)](http://travis-ci.org/mhuggins/ruby-measurement)
4
+
3
5
  [ruby-measurement](https://github.com/mhuggins/ruby-measurement) is a simple
4
6
  Ruby gem for calculating and converting units of measure.
5
7
 
data/Rakefile CHANGED
@@ -1,3 +1,6 @@
1
1
  require 'bundler/gem_tasks'
2
2
 
3
3
  Dir.glob('tasks/**/*.rake').each(&method(:import))
4
+
5
+ desc 'Default: run unit specs'
6
+ task default: :spec
@@ -2,3 +2,4 @@ require 'ruby-measurement/definitions/metric/area'
2
2
  require 'ruby-measurement/definitions/metric/capacity'
3
3
  require 'ruby-measurement/definitions/metric/length'
4
4
  require 'ruby-measurement/definitions/metric/volume'
5
+ require 'ruby-measurement/definitions/metric/weight'
@@ -1,89 +1,19 @@
1
1
  # encoding: UTF-8
2
2
 
3
- Measurement.define(:kl) do |unit|
4
- unit.alias :kiloliter, :kiloliters
5
- unit.convert_to(:hl) { |value| value * 10.0 }
6
- unit.convert_to(:dal) { |value| value * 100.0 }
7
- unit.convert_to(:l) { |value| value * 1000.0 }
8
- unit.convert_to(:dl) { |value| value * 10000.0 }
9
- unit.convert_to(:cl) { |value| value * 100000.0 }
10
- unit.convert_to(:ml) { |value| value * 1000000.0 }
11
- unit.convert_to(:µl) { |value| value * 10000000.0 }
3
+ Measurement.define(:'m³') do |unit|
4
+ unit.alias :m3, :'m^3', :'m*m*m', :'cubic meter', :'cubic meters'
5
+ unit.convert_to(:dm3) { |value| value * 1_000.0 }
6
+ unit.convert_to(:cm3) { |value| value * 1_000_000.0 }
12
7
  end
13
8
 
14
- Measurement.define(:hl) do |unit|
15
- unit.alias :hectoliter, :hectoliters
16
- unit.convert_to(:kl) { |value| value / 10.0 }
17
- unit.convert_to(:dal) { |value| value * 10.0 }
18
- unit.convert_to(:l) { |value| value * 100.0 }
19
- unit.convert_to(:dl) { |value| value * 1_000.0 }
20
- unit.convert_to(:cl) { |value| value * 10_000.0 }
21
- unit.convert_to(:ml) { |value| value * 100_000.0 }
22
- unit.convert_to(:µl) { |value| value * 1_000_000.0 }
9
+ Measurement.define(:'dm³') do |unit|
10
+ unit.alias :dm3, :'dm^3', :'dm*dm*dm', :'cubic decimeter', :'cubic decimeters'
11
+ unit.convert_to(:m3) { |value| value / 1_000.0 }
12
+ unit.convert_to(:cm3) { |value| value * 1_000.0 }
23
13
  end
24
14
 
25
- Measurement.define(:dal) do |unit|
26
- unit.alias :dekaliter, :dekaliters
27
- unit.convert_to(:kl) { |value| value / 100.0 }
28
- unit.convert_to(:hl) { |value| value / 10.0 }
29
- unit.convert_to(:l) { |value| value * 10.0 }
30
- unit.convert_to(:dl) { |value| value * 100.0 }
31
- unit.convert_to(:cl) { |value| value * 1_000.0 }
32
- unit.convert_to(:ml) { |value| value * 10_000.0 }
33
- unit.convert_to(:µl) { |value| value * 100_000.0 }
34
- end
35
-
36
- Measurement.define(:l) do |unit|
37
- unit.alias :liter, :liters
38
- unit.convert_to(:kl) { |value| value / 1_000.0 }
39
- unit.convert_to(:hl) { |value| value / 100.0 }
40
- unit.convert_to(:dal) { |value| value / 10.0 }
41
- unit.convert_to(:dl) { |value| value * 10.0 }
42
- unit.convert_to(:cl) { |value| value * 100.0 }
43
- unit.convert_to(:ml) { |value| value * 1_000.0 }
44
- unit.convert_to(:µl) { |value| value * 10_000.0 }
45
- end
46
-
47
- Measurement.define(:dl) do |unit|
48
- unit.alias :deciliter, :deciliters
49
- unit.convert_to(:kl) { |value| value / 10_000.0 }
50
- unit.convert_to(:hl) { |value| value / 1_000.0 }
51
- unit.convert_to(:dal) { |value| value / 100.0 }
52
- unit.convert_to(:l) { |value| value / 10.0 }
53
- unit.convert_to(:cl) { |value| value * 10.0 }
54
- unit.convert_to(:ml) { |value| value * 100.0 }
55
- unit.convert_to(:µl) { |value| value * 1_000.0 }
56
- end
57
-
58
- Measurement.define(:cl) do |unit|
59
- unit.alias :centiliter, :centiliters
60
- unit.convert_to(:kl) { |value| value / 10_0000.0 }
61
- unit.convert_to(:hl) { |value| value / 10_000.0 }
62
- unit.convert_to(:dal) { |value| value / 1_000.0 }
63
- unit.convert_to(:l) { |value| value / 100.0 }
64
- unit.convert_to(:dl) { |value| value / 10.0 }
65
- unit.convert_to(:ml) { |value| value * 10.0 }
66
- unit.convert_to(:µl) { |value| value * 100.0 }
67
- end
68
-
69
- Measurement.define(:ml) do |unit|
70
- unit.alias :milliliter, :milliliters
71
- unit.convert_to(:kl) { |value| value / 1_000_000.0 }
72
- unit.convert_to(:hl) { |value| value / 100_000.0 }
73
- unit.convert_to(:dal) { |value| value / 10_000.0 }
74
- unit.convert_to(:l) { |value| value / 1_000.0 }
75
- unit.convert_to(:dl) { |value| value / 100.0 }
76
- unit.convert_to(:cl) { |value| value / 10.0 }
77
- unit.convert_to(:µl) { |value| value * 10.0 }
78
- end
79
-
80
- Measurement.define(:µl) do |unit|
81
- unit.alias :microliter, :microliters
82
- unit.convert_to(:kl) { |value| value / 10_000_000.0 }
83
- unit.convert_to(:hl) { |value| value / 1_000_000.0 }
84
- unit.convert_to(:dal) { |value| value / 100_000.0 }
85
- unit.convert_to(:l) { |value| value / 10_000.0 }
86
- unit.convert_to(:dl) { |value| value / 1_000.0 }
87
- unit.convert_to(:cl) { |value| value / 100.0 }
88
- unit.convert_to(:ml) { |value| value / 10.0 }
15
+ Measurement.define(:'cm³') do |unit|
16
+ unit.alias :cm3, :'cm^3', :'cm*cm*cm', :'cubic centimeter', :'cubic centimeters'
17
+ unit.convert_to(:m3) { |value| value / 1_000_000.0 }
18
+ unit.convert_to(:dm3) { |value| value / 1_000.0 }
89
19
  end
@@ -1,19 +1,89 @@
1
1
  # encoding: UTF-8
2
2
 
3
- Measurement.define(:'m³') do |unit|
4
- unit.alias :m3, :'m^3', :'m*m*m', :'cubic meter', :'cubic meters'
5
- unit.convert_to(:dm3) { |value| value * 1_000.0 }
6
- unit.convert_to(:cm3) { |value| value * 1_000_000.0 }
3
+ Measurement.define(:kl) do |unit|
4
+ unit.alias :kiloliter, :kiloliters
5
+ unit.convert_to(:hl) { |value| value * 10.0 }
6
+ unit.convert_to(:dal) { |value| value * 100.0 }
7
+ unit.convert_to(:l) { |value| value * 1000.0 }
8
+ unit.convert_to(:dl) { |value| value * 10000.0 }
9
+ unit.convert_to(:cl) { |value| value * 100000.0 }
10
+ unit.convert_to(:ml) { |value| value * 1000000.0 }
11
+ unit.convert_to(:µl) { |value| value * 10000000.0 }
7
12
  end
8
13
 
9
- Measurement.define(:'dm³') do |unit|
10
- unit.alias :dm3, :'dm^3', :'dm*dm*dm', :'cubic decimeter', :'cubic decimeters'
11
- unit.convert_to(:m3) { |value| value / 1_000.0 }
12
- unit.convert_to(:cm3) { |value| value * 1_000.0 }
14
+ Measurement.define(:hl) do |unit|
15
+ unit.alias :hectoliter, :hectoliters
16
+ unit.convert_to(:kl) { |value| value / 10.0 }
17
+ unit.convert_to(:dal) { |value| value * 10.0 }
18
+ unit.convert_to(:l) { |value| value * 100.0 }
19
+ unit.convert_to(:dl) { |value| value * 1_000.0 }
20
+ unit.convert_to(:cl) { |value| value * 10_000.0 }
21
+ unit.convert_to(:ml) { |value| value * 100_000.0 }
22
+ unit.convert_to(:µl) { |value| value * 1_000_000.0 }
13
23
  end
14
24
 
15
- Measurement.define(:'cm³') do |unit|
16
- unit.alias :cm3, :'cm^3', :'cm*cm*cm', :'cubic centimeter', :'cubic centimeters'
17
- unit.convert_to(:m3) { |value| value / 1_000_000.0 }
18
- unit.convert_to(:dm3) { |value| value / 1_000.0 }
25
+ Measurement.define(:dal) do |unit|
26
+ unit.alias :dekaliter, :dekaliters
27
+ unit.convert_to(:kl) { |value| value / 100.0 }
28
+ unit.convert_to(:hl) { |value| value / 10.0 }
29
+ unit.convert_to(:l) { |value| value * 10.0 }
30
+ unit.convert_to(:dl) { |value| value * 100.0 }
31
+ unit.convert_to(:cl) { |value| value * 1_000.0 }
32
+ unit.convert_to(:ml) { |value| value * 10_000.0 }
33
+ unit.convert_to(:µl) { |value| value * 100_000.0 }
34
+ end
35
+
36
+ Measurement.define(:l) do |unit|
37
+ unit.alias :liter, :liters
38
+ unit.convert_to(:kl) { |value| value / 1_000.0 }
39
+ unit.convert_to(:hl) { |value| value / 100.0 }
40
+ unit.convert_to(:dal) { |value| value / 10.0 }
41
+ unit.convert_to(:dl) { |value| value * 10.0 }
42
+ unit.convert_to(:cl) { |value| value * 100.0 }
43
+ unit.convert_to(:ml) { |value| value * 1_000.0 }
44
+ unit.convert_to(:µl) { |value| value * 10_000.0 }
45
+ end
46
+
47
+ Measurement.define(:dl) do |unit|
48
+ unit.alias :deciliter, :deciliters
49
+ unit.convert_to(:kl) { |value| value / 10_000.0 }
50
+ unit.convert_to(:hl) { |value| value / 1_000.0 }
51
+ unit.convert_to(:dal) { |value| value / 100.0 }
52
+ unit.convert_to(:l) { |value| value / 10.0 }
53
+ unit.convert_to(:cl) { |value| value * 10.0 }
54
+ unit.convert_to(:ml) { |value| value * 100.0 }
55
+ unit.convert_to(:µl) { |value| value * 1_000.0 }
56
+ end
57
+
58
+ Measurement.define(:cl) do |unit|
59
+ unit.alias :centiliter, :centiliters
60
+ unit.convert_to(:kl) { |value| value / 10_0000.0 }
61
+ unit.convert_to(:hl) { |value| value / 10_000.0 }
62
+ unit.convert_to(:dal) { |value| value / 1_000.0 }
63
+ unit.convert_to(:l) { |value| value / 100.0 }
64
+ unit.convert_to(:dl) { |value| value / 10.0 }
65
+ unit.convert_to(:ml) { |value| value * 10.0 }
66
+ unit.convert_to(:µl) { |value| value * 100.0 }
67
+ end
68
+
69
+ Measurement.define(:ml) do |unit|
70
+ unit.alias :milliliter, :milliliters
71
+ unit.convert_to(:kl) { |value| value / 1_000_000.0 }
72
+ unit.convert_to(:hl) { |value| value / 100_000.0 }
73
+ unit.convert_to(:dal) { |value| value / 10_000.0 }
74
+ unit.convert_to(:l) { |value| value / 1_000.0 }
75
+ unit.convert_to(:dl) { |value| value / 100.0 }
76
+ unit.convert_to(:cl) { |value| value / 10.0 }
77
+ unit.convert_to(:µl) { |value| value * 10.0 }
78
+ end
79
+
80
+ Measurement.define(:µl) do |unit|
81
+ unit.alias :microliter, :microliters
82
+ unit.convert_to(:kl) { |value| value / 10_000_000.0 }
83
+ unit.convert_to(:hl) { |value| value / 1_000_000.0 }
84
+ unit.convert_to(:dal) { |value| value / 100_000.0 }
85
+ unit.convert_to(:l) { |value| value / 10_000.0 }
86
+ unit.convert_to(:dl) { |value| value / 1_000.0 }
87
+ unit.convert_to(:cl) { |value| value / 100.0 }
88
+ unit.convert_to(:ml) { |value| value / 10.0 }
19
89
  end
@@ -1,4 +1,5 @@
1
1
  require 'ruby-measurement/definitions/us_customary/area'
2
+ require 'ruby-measurement/definitions/us_customary/capacity'
2
3
  require 'ruby-measurement/definitions/us_customary/length'
3
4
  require 'ruby-measurement/definitions/us_customary/volume'
4
5
  require 'ruby-measurement/definitions/us_customary/weight'
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+
3
+ Measurement.define(:'acre ft') do |unit|
4
+ unit.alias :'acre-foot', :'acre-feet'
5
+ unit.convert_to(:yd3) { |value| value * Rational(4840, 3).to_f }
6
+ unit.convert_to(:ft3) { |value| value * 43_560.0 }
7
+ unit.convert_to(:in3) { |value| value * 75_271_680.0 }
8
+ end
9
+
10
+ Measurement.define(:'yd³') do |unit|
11
+ unit.alias :yd3, :'yd^3', :'yd*yd*yd', :'cubic yard', :'cubic yards'
12
+ unit.convert_to(:'acre ft') { |value| value / Rational(4840, 3).to_f }
13
+ unit.convert_to(:ft3) { |value| value * 27.0 }
14
+ unit.convert_to(:in3) { |value| value * 46_656.0 }
15
+ end
16
+
17
+ Measurement.define(:'ft³') do |unit|
18
+ unit.alias :ft3, :'ft^3', :'ft*ft*ft', :'cubic foot', :'cubic feet'
19
+ unit.convert_to(:'acre ft') { |value| value / 43_560.0 }
20
+ unit.convert_to(:yd3) { |value| value / 27.0 }
21
+ unit.convert_to(:in3) { |value| value * 1_728.0 }
22
+ end
23
+
24
+ Measurement.define(:'in³') do |unit|
25
+ unit.alias :in3, :'in^3', :'in*in*in', :'cubic inch', :'cubic inches'
26
+ unit.convert_to(:'acre ft') { |value| value / 75_271_680.0 }
27
+ unit.convert_to(:yd3) { |value| value / 46_656.0 }
28
+ unit.convert_to(:ft3) { |value| value / 1_728.0 }
29
+ end
@@ -1,20 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
- Measurement.define(:'lea.') do |unit|
4
- unit.alias :lea, :league, :leagues
5
- unit.convert_to(:mi) { |value| value * 3.45233834 }
6
- unit.convert_to(:fur) { |value| value * 27.6187068 }
7
- unit.convert_to(:ch) { |value| value * 276.187068 }
8
- unit.convert_to(:ftm) { |value| value * 3_038.05774 }
9
- unit.convert_to(:yd) { |value| value * 6_076.11549 }
10
- unit.convert_to(:ft) { |value| value * 18_228.3465 }
11
- unit.convert_to(:in) { |value| value * 218_740.157 }
12
- unit.convert_to(:th) { |value| value * 218_740_157.0 }
13
- end
14
-
15
3
  Measurement.define(:'mi.') do |unit|
16
4
  unit.alias :mi, :mile, :miles
17
- unit.convert_to(:lea) { |value| value / 3.45233834 }
18
5
  unit.convert_to(:fur) { |value| value * 8.0 }
19
6
  unit.convert_to(:ch) { |value| value * 80.0 }
20
7
  unit.convert_to(:ftm) { |value| value * 880.0 }
@@ -26,7 +13,6 @@ end
26
13
 
27
14
  Measurement.define(:'fur.') do |unit|
28
15
  unit.alias :fur, :furlong, :furlongs
29
- unit.convert_to(:lea) { |value| value / 27.6187068 }
30
16
  unit.convert_to(:mi) { |value| value / 8.0 }
31
17
  unit.convert_to(:ch) { |value| value * 10.0 }
32
18
  unit.convert_to(:ftm) { |value| value * 110.0 }
@@ -38,7 +24,6 @@ end
38
24
 
39
25
  Measurement.define(:'ch.') do |unit|
40
26
  unit.alias :ch, :chain, :chains
41
- unit.convert_to(:lea) { |value| value / 276.187068 }
42
27
  unit.convert_to(:mi) { |value| value / 80.0 }
43
28
  unit.convert_to(:fur) { |value| value / 10.0 }
44
29
  unit.convert_to(:ftm) { |value| value * 11.0 }
@@ -50,7 +35,6 @@ end
50
35
 
51
36
  Measurement.define(:'ftm.') do |unit|
52
37
  unit.alias :ftm, :fathom, :fathoms
53
- unit.convert_to(:lea) { |value| value / 3_038.05774 }
54
38
  unit.convert_to(:mi) { |value| value / 880.0 }
55
39
  unit.convert_to(:fur) { |value| value / 110.0 }
56
40
  unit.convert_to(:ch) { |value| value / 11.0 }
@@ -62,7 +46,6 @@ end
62
46
 
63
47
  Measurement.define(:'yd.') do |unit|
64
48
  unit.alias :yd, :yard, :yards
65
- unit.convert_to(:lea) { |value| value / 6_076.11549 }
66
49
  unit.convert_to(:mi) { |value| value / 1_760.0 }
67
50
  unit.convert_to(:fur) { |value| value / 220.0 }
68
51
  unit.convert_to(:ch) { |value| value / 22.0 }
@@ -74,7 +57,6 @@ end
74
57
 
75
58
  Measurement.define(:'ft.') do |unit|
76
59
  unit.alias :ft, :foot, :feet, :"'"
77
- unit.convert_to(:lea) { |value| value / 18_228.3465 }
78
60
  unit.convert_to(:mi) { |value| value / 5_280.0 }
79
61
  unit.convert_to(:ch) { |value| value / 66.0 }
80
62
  unit.convert_to(:fur) { |value| value / 660.0 }
@@ -86,7 +68,6 @@ end
86
68
 
87
69
  Measurement.define(:'in.') do |unit|
88
70
  unit.alias :in, :inch, :inches, :'"'
89
- unit.convert_to(:lea) { |value| value / 218_740.157 }
90
71
  unit.convert_to(:mi) { |value| value / 63_360.0 }
91
72
  unit.convert_to(:fur) { |value| value / 7_920.0 }
92
73
  unit.convert_to(:ch) { |value| value / 792.0 }
@@ -98,7 +79,6 @@ end
98
79
 
99
80
  Measurement.define(:'th.') do |unit|
100
81
  unit.alias :th, :thou
101
- unit.convert_to(:lea) { |value| value / 218_740_157.0 }
102
82
  unit.convert_to(:mi) { |value| value / 63_360_000.0 }
103
83
  unit.convert_to(:fur) { |value| value / 7_920_000.0 }
104
84
  unit.convert_to(:ch) { |value| value / 792_000.0 }
@@ -3,7 +3,7 @@
3
3
  Measurement.define(:ton) do |unit|
4
4
  unit.alias :'short ton', :'short tons', :tons
5
5
  unit.convert_to(:cwt) { |value| value * 20.0 }
6
- unit.convert_to(:lb) { |value| value * 2000.0 }
6
+ unit.convert_to(:lb) { |value| value * 2_000.0 }
7
7
  unit.convert_to(:oz) { |value| value * 32_000.0 }
8
8
  unit.convert_to(:dr) { |value| value * 512_000.0 }
9
9
  unit.convert_to(:gr) { |value| value * 14_000_000.0 }
@@ -14,8 +14,8 @@ Measurement.define(:cwt) do |unit|
14
14
  unit.convert_to(:ton) { |value| value / 20.0 }
15
15
  unit.convert_to(:lb) { |value| value * 100.0 }
16
16
  unit.convert_to(:oz) { |value| value * 1_600.0 }
17
- unit.convert_to(:dr) { |value| value * 28_672.0 }
18
- unit.convert_to(:gr) { |value| value * 784_000.0 }
17
+ unit.convert_to(:dr) { |value| value * 25_600.0 }
18
+ unit.convert_to(:gr) { |value| value * 700_000.0 }
19
19
  end
20
20
 
21
21
  Measurement.define(:lb) do |unit|
@@ -24,7 +24,7 @@ Measurement.define(:lb) do |unit|
24
24
  unit.convert_to(:cwt) { |value| value / 100.0 }
25
25
  unit.convert_to(:oz) { |value| value * 16.0 }
26
26
  unit.convert_to(:dr) { |value| value * 256.0 }
27
- unit.convert_to(:gr) { |value| value * 7000.0 }
27
+ unit.convert_to(:gr) { |value| value * 7_000.0 }
28
28
  end
29
29
 
30
30
  Measurement.define(:oz) do |unit|
@@ -39,7 +39,7 @@ end
39
39
  Measurement.define(:dr) do |unit|
40
40
  unit.alias :dram, :drams
41
41
  unit.convert_to(:ton) { |value| value / 512_000.0 }
42
- unit.convert_to(:cwt) { |value| value / 28_672.0 }
42
+ unit.convert_to(:cwt) { |value| value / 25_600.0 }
43
43
  unit.convert_to(:lb) { |value| value / 256.0 }
44
44
  unit.convert_to(:oz) { |value| value / 16.0 }
45
45
  unit.convert_to(:gr) { |value| value * 27.34375 }
@@ -48,7 +48,7 @@ end
48
48
  Measurement.define(:gr) do |unit|
49
49
  unit.alias :grain, :grains
50
50
  unit.convert_to(:ton) { |value| value / 14_000_000.0 }
51
- unit.convert_to(:cwt) { |value| value / 784_000.0 }
51
+ unit.convert_to(:cwt) { |value| value / 700_000.0 }
52
52
  unit.convert_to(:lb) { |value| value / 7000.0 }
53
53
  unit.convert_to(:oz) { |value| value / 437.5 }
54
54
  unit.convert_to(:dr) { |value| value / 27.34375 }
@@ -2,7 +2,7 @@ require 'ruby-measurement/unit'
2
2
  require 'ruby-measurement/version'
3
3
 
4
4
  class Measurement
5
- UNIT_REGEX = /([a-zA-Z].*)/.freeze
5
+ UNIT_REGEX = /([^\d\s\/].*)/.freeze
6
6
  SCIENTIFIC_NUMBER = /([+-]?\d*\.?\d+(?:[Ee][+-]?)?\d*)/.freeze
7
7
  SCIENTIFIC_REGEX = /\A#{SCIENTIFIC_NUMBER}\s*#{UNIT_REGEX}?\z/.freeze
8
8
  RATIONAL_REGEX = /\A([+-]?\d+\s+)?((\d+)\/(\d+))?\s*#{UNIT_REGEX}?\z/.freeze
@@ -10,12 +10,9 @@ class Measurement
10
10
 
11
11
  attr_reader :quantity, :unit
12
12
 
13
- class << self; attr_accessor :units end
14
- @units = {}
15
-
16
13
  def initialize(quantity, unit_name = :count)
17
14
  unit = unit_name
18
- unit = self.class.units[unit_name.to_s] if unit_name.kind_of?(Symbol) || unit_name.kind_of?(String)
15
+ unit = Unit[unit_name.to_s] if unit_name.kind_of?(Symbol) || unit_name.kind_of?(String)
19
16
 
20
17
  raise ArgumentError, "Invalid quantity: #{quantity}" unless quantity.kind_of?(Numeric)
21
18
  raise ArgumentError, "Invalid unit: #{unit_name}" unless unit.kind_of?(Unit)
@@ -106,7 +103,7 @@ class Measurement
106
103
  end
107
104
 
108
105
  def convert_to(unit_name)
109
- unit = self.class.unit(unit_name)
106
+ unit = Unit[unit_name]
110
107
  raise ArgumentError, "Invalid unit: '#{unit_name}'" unless unit
111
108
 
112
109
  return dup if unit == @unit
@@ -123,11 +120,6 @@ class Measurement
123
120
  self
124
121
  end
125
122
 
126
- def self.unit(unit_name)
127
- unit_name = unit_name.to_s
128
- @units.values.find { |unit| unit.aliases.include?(unit_name) }
129
- end
130
-
131
123
  def self.parse(str = '0')
132
124
  str = str.strip
133
125
 
@@ -152,15 +144,14 @@ class Measurement
152
144
  end
153
145
 
154
146
  unit_name ||= 'count'
155
- unit = units[unit_name.strip.downcase]
147
+ unit = Unit[unit_name.strip.downcase]
156
148
  raise ArgumentError, "Invalid unit: '#{unit_name}'" unless unit
157
149
 
158
150
  new(quantity, unit)
159
151
  end
160
152
 
161
153
  def self.define(unit_name, &block)
162
- unit = Unit.new(unit_name, &block)
163
- unit.aliases.each { |a| @units[a.downcase] = unit }
154
+ Unit.define(unit_name, &block)
164
155
  end
165
156
 
166
157
  define(:count) do |unit|