quantify 3.1.3 → 3.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -3,67 +3,36 @@ Quantify
3
3
 
4
4
  A gem to support physical quantities and unit conversions in Ruby
5
5
 
6
- Licensed under the MIT license (See COPYING file for details)
7
-
8
6
  Author: Andrew Berkeley (andrew.berkeley.is@googlemail.com)
9
7
 
10
- Homepage: https://github.com/spatchcock/quantify
8
+ Homepage: https://github.com/spatchcock/quantify
11
9
 
12
10
 
13
11
  Quick introduction
14
12
  ------------------
13
+ ```ruby
14
+ # Operating on quantities
15
15
 
16
- # Operating on quantities
17
-
18
- 12.feet + 12.feet #=> "24.0 feet"
19
-
20
- 6.m ** 2 #=> "36.0 m²"
21
-
22
- 100.km / 2.h #=> "50 kilometers per hour"
16
+ 12.feet + 12.feet #=> <Quantify::Quantity:0xb7332bbc ... >
17
+ _.to_s #=> "24.0 feet"
23
18
 
24
- 5000.L.to_bbl #=> "31.4490528488754 barrels"
19
+ 6.m ** 2 #=> <Quantify::Quantity:0xb7332bbc ... >
20
+ _.to_s #=> "36.0 m²"
25
21
 
26
- 1.5.lb.to_si.round(2) #=> "0.68 kg"
22
+ 100.km / 2.h #=> <Quantify::Quantity:0xb7332bbc ... >
23
+ _.to_s #=> "50 kilometers per hour"
27
24
 
28
- Unit.ratio :kg, :ton #=> "1016.047 kilograms per long ton"
25
+ 5000.L.to_bbl #=> <Quantify::Quantity:0xb7332bbc ... >
26
+ _.to_s #=> "31.4490528488754 barrels"
29
27
 
30
- Note: these results are string representations of the actual objects
31
- which result from these operations, using the Quantity#to_s method which
32
- renders quantities using either the unit name or symbol.
28
+ 1.5.lb.to_si.round(2) #=> <Quantify::Quantity:0xb7332bbc ... >
29
+ _.to_s #=> "0.68 kg"
33
30
 
34
- # Handling units
35
-
36
- Unit.ton.name #=> "long ton"
37
-
38
- Unit.ton.label #=> "ton_uk"
39
-
40
- Unit.ton.measures #=> "mass"
41
-
42
- Unit.ton.alternatives :name #=> [ "kilogram",
43
- "gram",
44
- "carat",
45
- "electron mass",
46
- "grain",
47
- "hundredweight long",
48
- "hundredweight short",
49
- "ounce",
50
- "pennyweight",
51
- "pound",
52
- "short ton",
53
- "stone",
54
- "tonne",
55
- "unified atomic mass" ]
56
-
57
- Unit.ton.si_unit #=> 'kg'
58
-
59
- Unit.ton.dimensions #=> <Quantify::Dimensions:0xb75467c8 ... >
60
-
61
- Unit.ton.dimensions.describe #=> "mass"
62
-
63
- Unit.ton.dimensions.mass #=> 1
64
-
65
- Unit.ton.dimensions.length #=> nil
31
+ 1.kg < 1.g #=> false
66
32
 
33
+ Unit.ratio(:kg, :ton) #=> <Quantify::Quantity:0xb7332bbc ... >
34
+ _.to_s #=> "1016.047 kilograms per long ton"
35
+ ```
67
36
 
68
37
  General introduction
69
38
  --------------------
@@ -78,74 +47,126 @@ Quantities can be manipulated and operated on, in all of the ways that might be
78
47
  In all cases the results of operations are a changes to the Quantity instance or a new instance of a Quantity object. The new value or unit can be accessed using the #value and #unit attributes, or the #to_s method which renders the quantity in string form.
79
48
 
80
49
  There are several ways to initialize a quantity object
81
-
82
- mass = Quantity.new(100,:lb) #=> <Quantify::Quantity:0xb7332bbc ... >
83
- mass = Quantity.new(100,'pound') #=> <Quantify::Quantity:0xb7332bbc ... >
84
- mass = 100.lb #=> <Quantify::Quantity:0xb7332bbc ... >
85
- mass = 100.pound #=> <Quantify::Quantity:0xb7332bbc ... >
86
- mass = Quantity.parse "100 lb" #=> <Quantify::Quantity:0xb7332bbc ... >
87
- mass = "100 lb".to_q #=> <Quantify::Quantity:0xb7332bbc ... >
50
+ ```ruby
51
+ long_text = "I travelled 220 miles driving my car and using 0.13 UK gallons per mile of diesel"
52
+
53
+ mass = Quantity.new(100,:lb) #=> <Quantify::Quantity:0xb7332bbc ... >
54
+ mass = Quantity.new(100,'pound') #=> <Quantify::Quantity:0xb7332bbc ... >
55
+ mass = 100.lb #=> <Quantify::Quantity:0xb7332bbc ... >
56
+ mass = 100.pound #=> <Quantify::Quantity:0xb7332bbc ... >
57
+ mass = Quantity.parse "100 lb" #=> [<Quantify::Quantity:0xb7332bbc ... >]
58
+ mass = Quantity.parse long_text #=> [<Quantify::Quantity:0xb7332bbc ... >, <Quantify::Quantity:0xc564321c ... >]
59
+ mass = "100 lb".to_q #=> [<Quantify::Quantity:0xb7332bbc ... >]
60
+ ```
88
61
 
89
62
  Quantity object can be insterrogated for a range of attributes
63
+ ```ruby
64
+ mass = 100.pound #=> <Quantify::Quantity:0xb7332bbc ... >
65
+
66
+ mass.value #=> 100.0
67
+ mass.unit #=> <Quantify::Unit::NonSI:0xb7332b08 ... >
68
+ mass.unit.name #=> "pound"
69
+ mass.unit.symbol #=> "lb"
70
+
71
+ mass.unit.label #=> "lb" # unique identifier, follows JScience
72
+ mass.unit.pluralized_name #=> "pounds"
73
+ mass.to_s #=> "100 lb"
74
+ mass.to_s(:name) #=> "100 pounds"
75
+
76
+ mass.represents #=> "mass" # Describe the physical quantity represented by the quantity
77
+ mass.unit.measures #=> "mass" # Describe the physical quantity described by the unit
78
+
79
+ mass.unit.alternatives(:name) #=> [ "kilogram",
80
+ # "gram",
81
+ # "carat",
82
+ # "electron mass",
83
+ # "grain",
84
+ # "hundredweight long",
85
+ # "hundredweight short",
86
+ # "ounce",
87
+ # "pennyweight",
88
+ # "pound",
89
+ # "short ton",
90
+ # "stone",
91
+ # "tonne",
92
+ # "unified atomic mass" ]
93
+
94
+ mass.unit.si_unit #=> 'kg'
90
95
 
91
- mass.value #=> 100.0
92
- mass.unit #=> <Quantify::Unit::NonSI:0xb7332b08 ... >
93
- mass.unit.name #=> "pound"
94
- mass.unit.symbol #=> "lb"
95
-
96
- # unique identifier, follows JScience
97
- mass.unit.label #=> "lb"
98
- mass.unit.pluralized_name #=> "pounds"
99
- mass.to_s #=> "100 lb"
100
- mass.to_s(:name) #=> "100 pounds"
101
-
102
- # Describe the physical quantity represented by the quantity
103
- mass.represents #=> "mass"
96
+ mass.unit.dimensions #=> <Quantify::Dimensions:0xb75467c8 ... >
97
+ mass.unit.dimensions.describe #=> "mass"
98
+ mass.unit.dimensions.mass #=> 1 # index of base dimension 'mass'
99
+ ```
104
100
 
105
101
  Convert a quantity to a different unit
106
-
107
- energy = 100.kWh #=> "100 kilowatt hours"
102
+ ```ruby
103
+ energy = 100.kWh #=> <Quantify::Quantity:0xb7332bbc ... >
104
+ energy.to_s #=> "100 kilowatt hours"
108
105
 
109
- new_energy = energy.to_megajoules #=> "360.0 MJ"
106
+ new_energy = energy.to_megajoules #=> <Quantify::Quantity:0xb7332bbc ... >
107
+ new_energy.to_s #=> "360.0 MJ"
110
108
 
111
- new_energy = energy.to_MJ #=> "360.0 MJ"
109
+ new_energy = energy.to_MJ #=> <Quantify::Quantity:0xb7332bbc ... >
110
+ new_energy.to_s #=> "360.0 MJ"
112
111
 
113
- new_energy = energy.to(:MJ) #=> "360.0 MJ"
112
+ new_energy = energy.to(:MJ) #=> <Quantify::Quantity:0xb7332bbc ... >
113
+ new_energy.to_s #=> "360.0 MJ"
114
114
 
115
- # Initialize a unit object and pass as conversion argument
116
- unit = Unit.MJ #=> <Quantify::Unit::SI:0xb75c9718 ... >
117
- new_energy = energy.to(unit) #=> "360.0 MJ"
115
+ # Initialize a unit object and pass as conversion argument
116
+ unit = Unit.MJ #=> <Quantify::Unit::SI:0xb75c9718 ... >
117
+ new_energy = energy.to(unit) #=> <Quantify::Quantity:0xb7332bbc ... >
118
+ new_energy.to_s #=> "360.0 MJ"
119
+ ```
118
120
 
119
121
  Convert the units of a quantity with a compound unit
122
+ ```ruby
123
+ speed = 70.mi/1.h #=> <Quantify::Quantity:0xb7332bbc ... >
124
+ speed.to_s #=> "70.0 mi/h"
120
125
 
121
- speed = 70.mi/1.h #=> "70.0 mi/h"
122
-
123
- speed_in_kms = speed.to_km #=> "112.65408 km/h"
126
+ speed_in_kms = speed.to_km #=> <Quantify::Quantity:0xb7332bbc ... >
127
+ speed_in_kms.to_s #=> "112.65408 km/h"
124
128
 
125
- speed_in_mins = speed_in_kms.to_min #=> "1.877568 km/min"
129
+ speed_in_mins = speed_in_kms.to_min #=> <Quantify::Quantity:0xb7332bbc ... >
130
+ speed_in_mins.to_s #=> "1.877568 km/min"
131
+ ```
126
132
 
127
133
  Convert a quantity to the corresponding SI unit
134
+ ```ruby
135
+ energy = 100.kWh #=> <Quantify::Quantity:0xb7332bbc ... >
136
+ energy.to_s #=> "100 kWh"
128
137
 
129
- energy = 100.kWh #=> "100 kWh"
130
- si = quantity.to_si #=> "360000000.0 J"
138
+ si = quantity.to_si #=> <Quantify::Quantity:0xb7332bbc ... >
139
+ si.to_s #=> "360000000.0 J"
140
+ ```
131
141
 
132
142
  Operate on a quantity
143
+ ```ruby
144
+ mass = 10.kg * 3 #=> <Quantify::Quantity:0xb7332bbc ... >
145
+ mass.to_s #=> "30.0 kg"
133
146
 
134
- mass = 10.kg * 3 #=> "30.0 kg"
147
+ distance = 100.light_years / 20 #=> <Quantify::Quantity:0xb7332bbc ... >
148
+ distance.to_s #=> "5.0 ly"
135
149
 
136
- distance = 100.light_years / 20 #=> "5.0 ly"
150
+ area = 10.m * 10.m #=> <Quantify::Quantity:0xb7332bbc ... >
151
+ area.to_s #=> "100.0 square metres"
137
152
 
138
- area = 10.m * 10.m #=> "100.0 square metres"
153
+ speed = 250.mi / 3.h #=> <Quantify::Quantity:0xb7332bbc ... >
154
+ speed.to_s #=> "83.3333333333333 miles per hour"
139
155
 
140
- speed = 250.mi / 3.h #=> "83.3333333333333 miles per hour"
156
+ speed = 70.mi/1.h #=> <Quantify::Quantity:0xb7332bbc ... >
157
+ time = 0.5.h #=> <Quantify::Quantity:3xf3472hjc ... >
158
+ distance = speed * time #=> <Quantify::Quantity:7d7f8g9d5g ... >
159
+ distance.to_s #=> "35.0 mi"
160
+ ```
141
161
 
142
- speed = 70.mi/1.h #=> <Quantify::Quantity:0xb7332bbc ... >
143
- time = 0.5.h #=> <Quantify::Quantity:3xf3472hjc ... >
144
- distance = speed * time #=> <Quantify::Quantity:7d7f8g9d5g ... >
145
- distance.to_s #=> "35.0 mi"
162
+ Compare quantities
163
+ ```ruby
164
+ 1.kg < 1.g #=> false
165
+ 1.kg > 1.g #=> true
146
166
 
147
- Note: all of the above results are string representations of the actual
148
- objects which result from these operations.
167
+ # Comparisons only valid if quantities are of same dimension
168
+ 1.kg < 1.m #=> <Quantify::Exceptions::InvalidArgumentError ... >
169
+ ```
149
170
 
150
171
  Additional operations
151
172
  ---------------------
@@ -157,18 +178,21 @@ Units are not automatically cancelled or rationalized (made consistent). This is
157
178
  and the user may not necessarily prefer a conversion into consistent mass units (e.g. g/g or t/t). Therefore, the following methods are available...
158
179
 
159
180
  Where units representing the same physical quantity appear together, they can be made consistent by simply converting to the unit which is desired:
160
-
161
- area = 12.yd * 36.ft #=> <Quantify::Quantity:0xb7332bbc ... >
162
- area.to_s #=> "432.0 yd ft"
163
- area.to_yd
164
- area.to_s #=> "144.0 yd²"
181
+ ```ruby
182
+ area = 12.yd * 36.ft #=> <Quantify::Quantity:0xb7332bbc ... >
183
+ area.to_s #=> "432.0 yd ft"
165
184
 
166
- Alternatively, all units within the numerator and denominator respectively can be standardized:
185
+ area = area.to_yd #=> <Quantify::Quantity:0xb7332bbc ... >
186
+ area.to_s #=> "144.0 yd²"
187
+ ```
167
188
 
168
- quantity = (12.ft*8.mi)/(1.s*8.min)
169
- quantity.to_s #=> 12.0 ft mi/s min
170
- quantity.rationalize_units!
171
- quantity.to_s #=> 1056.0 ft²/s²
189
+ Alternatively, all units within the numerator and denominator respectively can be standardized:
190
+ ```ruby
191
+ quantity = (12.ft*8.mi)/(1.s*8.min) #=> <Quantify::Quantity:0xb7332bbc ... >
192
+ quantity.to_s #=> "12.0 ft mi/s min"
193
+ quantity.rationalize_units!
194
+ quantity.to_s #=> "1056.0 ft²/s²"
195
+ ```
172
196
 
173
197
  A quantity with arbitrary cancelable units can be cancelled manually:
174
198
 
@@ -177,4 +201,22 @@ A quantity with arbitrary cancelable units can be cancelled manually:
177
201
  quantity.cancel_base_units! :m
178
202
  quantity.to_s #=> "746496.0 m^4"
179
203
 
180
- Note: there are more comprehensive and flexible methods for manupulating compound units available as part of of the class Unit::Compound. These can be used to convert a conpound unit into the precise form required. If such an approach is used, any quantity object can be converted to the new form by simply passing the new unit object into the Quantity#to method.
204
+ Note: there are more comprehensive and flexible methods for manupulating compound units available as part of of the class Unit::Compound. These can be used to convert a conpound unit into the precise form required. If such an approach is used, any quantity object can be converted to the new form by simply passing the new unit object into the Quantity#to method.
205
+
206
+ Contributing
207
+ ============
208
+
209
+ If you find a bug or think that you improve on the code, feel free to contribute.
210
+
211
+ You can:
212
+
213
+ * Send the author a message (andrew.berkeley.is@googlemail.com)
214
+ * Create an issue
215
+ * Fork the project and submit a pull request.
216
+
217
+ License
218
+ =======
219
+
220
+ © Copyright 2012 Andrew Berkeley.
221
+
222
+ Licensed under the MIT license (See COPYING file for details)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.1.3
1
+ 3.1.4
@@ -313,12 +313,15 @@ module Quantify
313
313
 
314
314
  def multiply_or_divide!(operator,other)
315
315
  if other.kind_of? Numeric
316
+ raise ZeroDivisionError if (other.to_f == 0.0 && operator == :/)
316
317
  @value = @value.send(operator,other)
318
+
317
319
  return self
318
320
  elsif other.kind_of? Quantity
319
321
  @unit = @unit.send(operator,other.unit).or_equivalent
320
322
  @unit.consolidate_base_units! if @unit.is_compound_unit? && Quantity.auto_consolidate_units?
321
323
  @value = @value.send(operator,other.value)
324
+
322
325
  return self
323
326
  else
324
327
  raise Quantify::Exceptions::InvalidArgumentError, "Cannot multiply or divide a Quantity by a non-Quantity or non-Numeric object"
data/quantify.gemspec CHANGED
@@ -5,18 +5,17 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "quantify"
8
- s.version = "3.1.3"
8
+ s.version = "3.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andrew Berkeley"]
12
- s.date = "2012-05-01"
12
+ s.date = "2012-10-30"
13
13
  s.description = "A gem to support physical quantities and unit conversions"
14
14
  s.email = "andrew.berkeley.is@googlemail.com"
15
15
  s.extra_rdoc_files = [
16
16
  "README.md"
17
17
  ]
18
18
  s.files = [
19
- ".rvmrc",
20
19
  "COPYING",
21
20
  "Gemfile",
22
21
  "README.md",
@@ -54,7 +53,7 @@ Gem::Specification.new do |s|
54
53
  s.homepage = "https://github.com/spatchcock/quantify"
55
54
  s.licenses = ["MIT"]
56
55
  s.require_paths = ["lib"]
57
- s.rubygems_version = "1.8.17"
56
+ s.rubygems_version = "1.8.24"
58
57
  s.summary = "Support for handling physical quantities, unit conversions, etc"
59
58
 
60
59
  if s.respond_to? :specification_version then
@@ -369,6 +369,18 @@ describe Quantity do
369
369
  (20.metre / 5).to_s.should == "4.0 m"
370
370
  end
371
371
 
372
+ it "should throw ZeroDivisionError error when dividing a quantity by '0'" do
373
+ lambda{2.kg / 0}.should raise_error ZeroDivisionError
374
+ end
375
+
376
+ it "should NOT throw ZeroDivisionError error when multiplying a quantity by '0'" do
377
+ lambda{2.kg * 0}.should_not raise_error ZeroDivisionError
378
+ end
379
+
380
+ it "should multiply a quantity by '0'" do
381
+ (2.kg * 0).to_s.should == "0.0 kg"
382
+ end
383
+
372
384
  it "should successfully divide a quantity by a scalar" do
373
385
  (2.kg / 0.5).round.to_s.should == "4 kg"
374
386
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quantify
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 3
8
8
  - 1
9
- - 3
10
- version: 3.1.3
9
+ - 4
10
+ version: 3.1.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Berkeley
@@ -15,12 +15,10 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-05-01 00:00:00 Z
18
+ date: 2012-10-30 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: activesupport
22
21
  prerelease: false
23
- type: :runtime
24
22
  requirement: &id001 !ruby/object:Gem::Requirement
25
23
  none: false
26
24
  requirements:
@@ -31,11 +29,11 @@ dependencies:
31
29
  - 3
32
30
  - 0
33
31
  version: "3.0"
32
+ type: :runtime
33
+ name: activesupport
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
36
- name: i18n
37
36
  prerelease: false
38
- type: :runtime
39
37
  requirement: &id002 !ruby/object:Gem::Requirement
40
38
  none: false
41
39
  requirements:
@@ -45,11 +43,11 @@ dependencies:
45
43
  segments:
46
44
  - 0
47
45
  version: "0"
46
+ type: :runtime
47
+ name: i18n
48
48
  version_requirements: *id002
49
49
  - !ruby/object:Gem::Dependency
50
- name: bundler
51
50
  prerelease: false
52
- type: :development
53
51
  requirement: &id003 !ruby/object:Gem::Requirement
54
52
  none: false
55
53
  requirements:
@@ -61,11 +59,11 @@ dependencies:
61
59
  - 0
62
60
  - 0
63
61
  version: 1.0.0
62
+ type: :development
63
+ name: bundler
64
64
  version_requirements: *id003
65
65
  - !ruby/object:Gem::Dependency
66
- name: jeweler
67
66
  prerelease: false
68
- type: :development
69
67
  requirement: &id004 !ruby/object:Gem::Requirement
70
68
  none: false
71
69
  requirements:
@@ -77,11 +75,11 @@ dependencies:
77
75
  - 6
78
76
  - 4
79
77
  version: 1.6.4
78
+ type: :development
79
+ name: jeweler
80
80
  version_requirements: *id004
81
81
  - !ruby/object:Gem::Dependency
82
- name: rspec
83
82
  prerelease: false
84
- type: :development
85
83
  requirement: &id005 !ruby/object:Gem::Requirement
86
84
  none: false
87
85
  requirements:
@@ -93,11 +91,11 @@ dependencies:
93
91
  - 6
94
92
  - 0
95
93
  version: 2.6.0
94
+ type: :development
95
+ name: rspec
96
96
  version_requirements: *id005
97
97
  - !ruby/object:Gem::Dependency
98
- name: rcov
99
98
  prerelease: false
100
- type: :development
101
99
  requirement: &id006 !ruby/object:Gem::Requirement
102
100
  none: false
103
101
  requirements:
@@ -107,11 +105,11 @@ dependencies:
107
105
  segments:
108
106
  - 0
109
107
  version: "0"
108
+ type: :development
109
+ name: rcov
110
110
  version_requirements: *id006
111
111
  - !ruby/object:Gem::Dependency
112
- name: rdoc
113
112
  prerelease: false
114
- type: :development
115
113
  requirement: &id007 !ruby/object:Gem::Requirement
116
114
  none: false
117
115
  requirements:
@@ -121,6 +119,8 @@ dependencies:
121
119
  segments:
122
120
  - 0
123
121
  version: "0"
122
+ type: :development
123
+ name: rdoc
124
124
  version_requirements: *id007
125
125
  description: A gem to support physical quantities and unit conversions
126
126
  email: andrew.berkeley.is@googlemail.com
@@ -131,7 +131,6 @@ extensions: []
131
131
  extra_rdoc_files:
132
132
  - README.md
133
133
  files:
134
- - .rvmrc
135
134
  - COPYING
136
135
  - Gemfile
137
136
  - README.md
@@ -194,7 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
193
  requirements: []
195
194
 
196
195
  rubyforge_project:
197
- rubygems_version: 1.8.17
196
+ rubygems_version: 1.8.24
198
197
  signing_key:
199
198
  specification_version: 3
200
199
  summary: Support for handling physical quantities, unit conversions, etc
data/.rvmrc DELETED
@@ -1 +0,0 @@
1
- rvm 1.8.7@quantify