quantify 3.1.1 → 3.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,19 @@
1
- A gem to support physical quantities and unit conversions
1
+ Quantify
2
+ ========
3
+
4
+ A gem to support physical quantities and unit conversions in Ruby
5
+
2
6
  Licensed under the MIT license (See COPYING file for details)
7
+
3
8
  Author: Andrew Berkeley (andrew.berkeley.is@googlemail.com)
9
+
4
10
  Homepage: https://github.com/spatchcock/quantify
5
11
 
12
+
6
13
  Quick introduction
14
+ ------------------
7
15
 
8
- # Quantity operations
16
+ Operating on quantities
9
17
 
10
18
  12.feet + 12.feet => "24.0 feet"
11
19
 
@@ -19,11 +27,11 @@ Quick introduction
19
27
 
20
28
  Unit.ratio :kg, :ton => "1.016047 kilograms per long ton"
21
29
 
22
- # Note: these results are string representations of the actual objects
23
- # which result from these operations, using the Quantity#to_s method which
24
- # renders quantities using either the unit name or symbol.
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.
25
33
 
26
- # Handling units
34
+ Handling units
27
35
 
28
36
  Unit.ton.name => "long ton"
29
37
 
@@ -58,27 +66,17 @@ Quick introduction
58
66
 
59
67
 
60
68
  General introduction
69
+ --------------------
61
70
 
62
71
  Quantify represents physical quantities using the Quantify::Quantity class.
63
72
 
64
73
  A Quantity object holds both a value (Numeric) and a unit (of the class
65
- Quantify::Unit::Base), for example a Quantity object might represent 12 kgs
66
- (value, 12; unit, kilogram).
67
-
68
- Quantities can be manipulated and operated on, in all of the ways that might be
69
- required for real physical quantities. Operations include, addition, subtraction,
70
- multiplying and dividing by scalar values or other quantities, raising to powers,
71
- converting into alternative unit representations (e.g. kgs to lbs, miles per hour
72
- to metres per second), and rounding of values. Quantify handles the converting of
73
- both values and units so that the result is a an accurate representation of the
74
- operation. For example, multiplying 10 metres by 10 metres will result in a quanity
75
- of square metres, whereas dividing, say, 10 metres by 2 seconds will result in a
76
- quantity in metres per second.
77
-
78
- In all cases the results of operations are a changes to the Quantity instance or
79
- a new instance of a Quantity object. The new value or unit can be accessed using
80
- the #value and #unit attributes, or the #to_s method which renders the quantity
81
- in string form.
74
+ Quantify::Unit::Base), for example a Quantity object might represent 12 kgs (value, 12; unit, kilogram).
75
+
76
+ Quantities can be manipulated and operated on, in all of the ways that might be required for real physical quantities. Operations include, addition, subtraction, multiplying and dividing by scalar values or other quantities, raising to powers, converting into alternative unit representations (e.g. kgs to lbs, miles per hour to metres per second), and rounding of values. Quantify handles the converting of both values and units so that the result is a an accurate representation of the
77
+ operation. For example, multiplying 10 metres by 10 metres will result in a quanity of square metres, whereas dividing, say, 10 metres by 2 seconds will result in a quantity in metres per second.
78
+
79
+ 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.
82
80
 
83
81
  There are several ways to initialize a quantity object
84
82
 
@@ -119,8 +117,7 @@ Convert a quantity to a different unit
119
117
  unit = Unit.MJ => <Quantify::Unit::SI:0xb75c9718 ... >
120
118
  new_energy = energy.to(unit) => "360.0 MJ"
121
119
 
122
- # Note: all of the above results are string representations of the actual
123
- # objects which result from these operations.
120
+ Note: all of the above results are string representations of the actual objects which result from these operations.
124
121
 
125
122
  Convert the units of a quantity with a compound unit
126
123
 
@@ -130,16 +127,15 @@ Convert the units of a quantity with a compound unit
130
127
 
131
128
  speed_in_mins = speed_in_kms.to_min => "1.877568 km/min"
132
129
 
133
- # Note: all of the above results are string representations of the actual
134
- # objects which result from these operations.
130
+ Note: all of the above results are string representations of the actual objects which result from these operations.
135
131
 
136
132
  Convert a quantity to the corresponding SI unit
137
133
 
138
134
  energy = 100.kWh => "100 kWh"
139
135
  si = quantity.to_si => "360000000.0 J"
140
136
 
141
- # Note: all of the above results are string representations of the actual
142
- # objects which result from these operations.
137
+ Note: all of the above results are string representations of the actual
138
+ objects which result from these operations.
143
139
 
144
140
  Operate on a quantity
145
141
 
@@ -156,52 +152,41 @@ Operate on a quantity
156
152
  distance = speed * time => <Quantify::Quantity:7d7f8g9d5g ... >
157
153
  distance.to_s => "35.0 mi"
158
154
 
159
- # Note: all of the above results are string representations of the actual
160
- # objects which result from these operations.
155
+ Note: all of the above results are string representations of the actual
156
+ objects which result from these operations.
161
157
 
162
158
  Additional operations
163
- The result of quantity operations is commonly a new quantity with a compound unit.
164
- Unless the result is equivalent to one of the base SI units (m, kg, s, K, etc.) or
165
- one of the following, square metre, cubic metre, joule, watt, newton or pascal, then
166
- the compound unit represents appropriate combination of the units involved, albeit
167
- with any like-units within the numerator and denominator grouped under a single
159
+ The result of quantity operations is commonly a new quantity with a compound unit. Unless the result is equivalent to one of the base SI units (m, kg, s, K, etc.) or one of the following, square metre, cubic metre, joule, watt, newton or pascal, then the compound unit represents appropriate combination of the units involved, albeit with any like-units within the numerator and denominator grouped under a single
168
160
  power/index.
169
161
 
170
- Units are not automatically cancelled or rationalized (made consistent). This is
171
- because it cannot be assumed that that is the desire of the user. For example, a
172
- quantity with units <mass> per <mass> is technically dimensionless, but the user
173
- might prefer to explicitly represent the units rather than reduce to a dimensionless
174
- quantity. In addition, this quantity might be expressed in terms of grams per tonne,
175
- and the user may not necessarily prefer a conversion into consistent mass units
176
- (e.g. g/g or t/t). Therefore, the following methods are available...
162
+ Units are not automatically cancelled or rationalized (made consistent). This is because it cannot be assumed that that is the desire of the user. For example, a quantity with units <mass> per <mass> is technically dimensionless, but the user might prefer to explicitly represent the units rather than reduce to a dimensionless quantity. In addition, this quantity might be expressed in terms of grams per tonne,
163
+ 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...
164
+
165
+ Where units representing the same physical quantity appear together, they can be made consistent by simply converting to the unit which is desired:
177
166
 
178
- # Where units representing the same physical quantity appear together, they can
179
- # made consistent by simply converting to the unit which is desired
180
167
  area = 12.yd * 36.ft => <Quantify::Quantity:0xb7332bbc ... >
181
168
  area.to_s => "432.0 yd ft"
182
169
  area.to_yd
183
170
  area.to_s => "144.0 yd²"
184
171
 
185
- # Alternatively, all units within the numerator and denominator respectively
186
- # can be standardized.
172
+ Alternatively, all units within the numerator and denominator respectively can be standardized:
173
+
187
174
  quantity = (12.ft*8.mi)/(1.s*8.min)
188
175
  quantity.to_s => 12.0 ft mi/s min
189
176
  quantity.rationalize_units!
190
177
  quantity.to_s => 1056.0 ft²/s²
191
178
 
192
- # A quantity with arbitrary cancelable units can be cancelled manually
179
+ A quantity with arbitrary cancelable units can be cancelled manually:
180
+
193
181
  quantity = (12.m**6) / 2.m**2
194
182
  quantity.to_s => "746496.0 m^6/m²"
195
183
  quantity.cancel_base_units! :m
196
184
  quantity.to_s => "746496.0 m^4"
197
185
 
198
- Note: there are more comprehensive and flexible methods for manupulating compound
199
- units available as part of of the class Unit::Compound. These can be used to
200
- convert a conpound unit into the precise form required. If such an approach is
201
- used, any quantity object can be converted to the new form by simply passing the
202
- new unit object into the Quantity#to method.
186
+ 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.
203
187
 
204
- Initialize a unit object
188
+ Initializing unit objects
189
+ -------------------------
205
190
 
206
191
  unit = Unit.for :km => <Quantify::Unit::SI:0xb75c9718 ... >
207
192
  unit = Unit.km => <Quantify::Unit::SI:0xb75c9718 ... >
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.1.1
1
+ 3.1.2
@@ -0,0 +1,5 @@
1
+ if RUBY_VERSION < "1.9"
2
+ class Range
3
+ alias :cover? include?
4
+ end
5
+ end
@@ -352,14 +352,14 @@ module Quantify
352
352
  # quantities which represent a quantity of something *per unit mass*
353
353
  #
354
354
  def is_specific_quantity?
355
- denominator_quantities == ["@mass"]
355
+ denominator_quantities == [:@mass]
356
356
  end
357
357
 
358
358
  # Method for identifying quantities which are 'molar' quantities, i.e
359
359
  # quantities which represent a quantity of something *per mole*
360
360
  #
361
361
  def is_molar_quantity?
362
- denominator_quantities == ["@amount_of_substance"]
362
+ denominator_quantities == [:@amount_of_substance]
363
363
  end
364
364
 
365
365
 
@@ -456,10 +456,11 @@ module Quantify
456
456
  quantities = self.instance_variables
457
457
  if RUBY_VERSION < "1.9"
458
458
  quantities.delete("@physical_quantity")
459
+ return quantities.map(&:to_sym)
459
460
  else
460
461
  quantities.delete(:@physical_quantity)
462
+ return quantities
461
463
  end
462
- return quantities
463
464
  end
464
465
 
465
466
  # Just the base quantities which have positive indices
@@ -251,7 +251,7 @@ module Quantify
251
251
 
252
252
  def ===(range)
253
253
  raise Exceptions::InvalidArgumentError unless range.is_a? Range
254
- range.include? self
254
+ range.cover? self
255
255
  end
256
256
 
257
257
  protected
@@ -297,7 +297,7 @@ module Quantify
297
297
  old_unit = @unit
298
298
  self.multiply!(Unit.ratio new_unit, old_unit)
299
299
  old_base_units = old_unit.base_units.map { |base| base.unit } if old_unit.is_compound_unit?
300
- self.cancel_base_units!(*old_base_units || old_unit)
300
+ self.cancel_base_units!(*old_base_units || [old_unit])
301
301
  end
302
302
 
303
303
  def conversion_with_scalings!(new_unit)
data/lib/quantify.rb CHANGED
@@ -11,6 +11,7 @@ require 'quantify/quantify'
11
11
  require 'quantify/core_extensions/string'
12
12
  require 'quantify/core_extensions/symbol'
13
13
  require 'quantify/core_extensions/numeric'
14
+ require 'quantify/core_extensions/range'
14
15
  require 'quantify/inflections'
15
16
  require 'quantify/exception'
16
17
  require 'quantify/dimensions'
data/quantify.gemspec CHANGED
@@ -5,26 +5,27 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "quantify"
8
- s.version = "3.1.1"
8
+ s.version = "3.1.2"
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 = "2011-12-22"
12
+ s.date = "2012-01-18"
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
- "README"
16
+ "README.md"
17
17
  ]
18
18
  s.files = [
19
19
  ".rvmrc",
20
20
  "COPYING",
21
21
  "Gemfile",
22
- "README",
22
+ "README.md",
23
23
  "Rakefile",
24
24
  "VERSION",
25
25
  "lib/quantify.rb",
26
26
  "lib/quantify/config.rb",
27
27
  "lib/quantify/core_extensions/numeric.rb",
28
+ "lib/quantify/core_extensions/range.rb",
28
29
  "lib/quantify/core_extensions/string.rb",
29
30
  "lib/quantify/core_extensions/symbol.rb",
30
31
  "lib/quantify/dimensions.rb",
@@ -53,7 +54,7 @@ Gem::Specification.new do |s|
53
54
  s.homepage = "https://github.com/spatchcock/quantify"
54
55
  s.licenses = ["MIT"]
55
56
  s.require_paths = ["lib"]
56
- s.rubygems_version = "1.8.10"
57
+ s.rubygems_version = "1.8.15"
57
58
  s.summary = "Support for handling physical quantities, unit conversions, etc"
58
59
 
59
60
  if s.respond_to? :specification_version then
@@ -364,9 +364,9 @@ describe Quantity do
364
364
  time_in_hours = time_in_min.to_hours
365
365
  speed = distance_in_miles / time_in_hours
366
366
  speed.class.should == Quantity
367
+ # use #be_within to tolerate Ruby 1.8.7 - 1.9.2 differences
367
368
  speed.value.should be_within(1.0e-08).of(27.1143792976291)
368
- speed.to_s(:name).should == "27.1143792976291 miles per hour"
369
- speed.to_s.should == "27.1143792976291 mi/h"
369
+ speed.unit.pluralized_name.should eql "miles per hour"
370
370
  end
371
371
 
372
372
  it "coerce method should handle inverted syntax" do
@@ -388,11 +388,17 @@ describe Quantity do
388
388
  end
389
389
 
390
390
  it "should convert standard units correctly" do
391
- 6000.BTU.to_megajoules.to_s(:name).should == "6.328824 megajoules"
391
+ quantity = 6000.BTU.to_megajoules
392
+ # use #be_within to tolerate Ruby 1.8.7 - 1.9.2 differences
393
+ quantity.value.should be_within(1.0e-08).of(6.328824)
394
+ quantity.unit.pluralized_name.should eql "megajoules"
392
395
  end
393
396
 
394
397
  it "should convert standard units correctly" do
395
- 13.1.stones.to_kg.to_s(:name).should == "83.1888383 kilograms"
398
+ quantity = 13.1.stones.to_kg
399
+ # use #be_within to tolerate Ruby 1.8.7 - 1.9.2 differences
400
+ quantity.value.should be_within(1.0e-08).of(83.1888383)
401
+ quantity.unit.pluralized_name.should eql "kilograms"
396
402
  end
397
403
 
398
404
  it "should convert compound units correctly" do
@@ -402,7 +408,12 @@ describe Quantity do
402
408
 
403
409
  it "should convert to SI unit correctly" do
404
410
  100.cm.to_si.to_s.should == "1.0 m"
405
- 2.kWh.to_si.to_s.should == "7200000.0 J"
411
+
412
+ quantity = 2.kWh.to_si
413
+ # use #be_within to tolerate Ruby 1.8.7 - 1.9.2 differences
414
+ quantity.value.should be_within(1.0e-08).of(7200000.0)
415
+ quantity.unit.symbol.should eql "J"
416
+
406
417
  400.ha.to_si.to_s.should == "4000000.0 m²"
407
418
  35.degree_celsius.to_si.to_s.should == "308.15 K"
408
419
  end
@@ -455,7 +466,9 @@ describe Quantity do
455
466
  it "should cancel by base units of original compound unit if necessary" do
456
467
  quantity = Quantity.new(20, Unit.psi).to(Unit.inches_of_mercury)
457
468
  quantity.unit.base_units.size.should == 1
458
- quantity.to_s.should == "40.720412743579 inHg"
469
+ # use #be_within to tolerate Ruby 1.8.7 - 1.9.2 differences
470
+ quantity.value.should be_within(1.0e-08).of(40.720412743579)
471
+ quantity.unit.symbol.should eql "inHg"
459
472
  end
460
473
 
461
474
  it "should rationalize units and return new quantity" do
@@ -512,13 +525,13 @@ describe Quantity do
512
525
  end
513
526
 
514
527
  it "should return between value from range" do
515
- (2.ft..20.ft).include?(3.ft).should be_true
528
+ (2.ft..20.ft).send(RUBY_VERSION < "1.9" ? :include? : :cover?, 3.ft).should be_true
516
529
  end
517
530
 
518
531
  it "should return between value from range with different units" do
519
- (2.ft..4.m).include?(200.cm).should be_true
520
- (1.ly..1.parsec).include?(2.ly).should be_true
521
- (1.ly..1.parsec).include?(2.in).should be_false
532
+ (2.ft..4.m).send(RUBY_VERSION < "1.9" ? :include? : :cover?, 200.cm).should be_true
533
+ (1.ly..1.parsec).send(RUBY_VERSION < "1.9" ? :include? : :cover?, 2.ly).should be_true
534
+ (1.ly..1.parsec).send(RUBY_VERSION < "1.9" ? :include? : :cover?, 2.in).should be_false
522
535
  end
523
536
 
524
537
  it "should return between value from range using === operator" do
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: 1
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 3
8
8
  - 1
9
- - 1
10
- version: 3.1.1
9
+ - 2
10
+ version: 3.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Berkeley
@@ -15,9 +15,10 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-12-22 00:00:00 Z
18
+ date: 2012-01-18 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
+ name: activesupport
21
22
  requirement: &id001 !ruby/object:Gem::Requirement
22
23
  none: false
23
24
  requirements:
@@ -28,11 +29,11 @@ dependencies:
28
29
  - 3
29
30
  - 0
30
31
  version: "3.0"
31
- version_requirements: *id001
32
- name: activesupport
33
- prerelease: false
34
32
  type: :runtime
33
+ prerelease: false
34
+ version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
36
+ name: i18n
36
37
  requirement: &id002 !ruby/object:Gem::Requirement
37
38
  none: false
38
39
  requirements:
@@ -42,11 +43,11 @@ dependencies:
42
43
  segments:
43
44
  - 0
44
45
  version: "0"
45
- version_requirements: *id002
46
- name: i18n
47
- prerelease: false
48
46
  type: :runtime
47
+ prerelease: false
48
+ version_requirements: *id002
49
49
  - !ruby/object:Gem::Dependency
50
+ name: bundler
50
51
  requirement: &id003 !ruby/object:Gem::Requirement
51
52
  none: false
52
53
  requirements:
@@ -58,11 +59,11 @@ dependencies:
58
59
  - 0
59
60
  - 0
60
61
  version: 1.0.0
61
- version_requirements: *id003
62
- name: bundler
63
- prerelease: false
64
62
  type: :development
63
+ prerelease: false
64
+ version_requirements: *id003
65
65
  - !ruby/object:Gem::Dependency
66
+ name: jeweler
66
67
  requirement: &id004 !ruby/object:Gem::Requirement
67
68
  none: false
68
69
  requirements:
@@ -74,11 +75,11 @@ dependencies:
74
75
  - 6
75
76
  - 4
76
77
  version: 1.6.4
77
- version_requirements: *id004
78
- name: jeweler
79
- prerelease: false
80
78
  type: :development
79
+ prerelease: false
80
+ version_requirements: *id004
81
81
  - !ruby/object:Gem::Dependency
82
+ name: rspec
82
83
  requirement: &id005 !ruby/object:Gem::Requirement
83
84
  none: false
84
85
  requirements:
@@ -90,11 +91,11 @@ dependencies:
90
91
  - 6
91
92
  - 0
92
93
  version: 2.6.0
93
- version_requirements: *id005
94
- name: rspec
95
- prerelease: false
96
94
  type: :development
95
+ prerelease: false
96
+ version_requirements: *id005
97
97
  - !ruby/object:Gem::Dependency
98
+ name: rcov
98
99
  requirement: &id006 !ruby/object:Gem::Requirement
99
100
  none: false
100
101
  requirements:
@@ -104,11 +105,11 @@ dependencies:
104
105
  segments:
105
106
  - 0
106
107
  version: "0"
107
- version_requirements: *id006
108
- name: rcov
109
- prerelease: false
110
108
  type: :development
109
+ prerelease: false
110
+ version_requirements: *id006
111
111
  - !ruby/object:Gem::Dependency
112
+ name: rdoc
112
113
  requirement: &id007 !ruby/object:Gem::Requirement
113
114
  none: false
114
115
  requirements:
@@ -118,10 +119,9 @@ dependencies:
118
119
  segments:
119
120
  - 0
120
121
  version: "0"
121
- version_requirements: *id007
122
- name: rdoc
123
- prerelease: false
124
122
  type: :development
123
+ prerelease: false
124
+ version_requirements: *id007
125
125
  description: A gem to support physical quantities and unit conversions
126
126
  email: andrew.berkeley.is@googlemail.com
127
127
  executables: []
@@ -129,17 +129,18 @@ executables: []
129
129
  extensions: []
130
130
 
131
131
  extra_rdoc_files:
132
- - README
132
+ - README.md
133
133
  files:
134
134
  - .rvmrc
135
135
  - COPYING
136
136
  - Gemfile
137
- - README
137
+ - README.md
138
138
  - Rakefile
139
139
  - VERSION
140
140
  - lib/quantify.rb
141
141
  - lib/quantify/config.rb
142
142
  - lib/quantify/core_extensions/numeric.rb
143
+ - lib/quantify/core_extensions/range.rb
143
144
  - lib/quantify/core_extensions/string.rb
144
145
  - lib/quantify/core_extensions/symbol.rb
145
146
  - lib/quantify/dimensions.rb
@@ -193,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
194
  requirements: []
194
195
 
195
196
  rubyforge_project:
196
- rubygems_version: 1.8.10
197
+ rubygems_version: 1.8.15
197
198
  signing_key:
198
199
  specification_version: 3
199
200
  summary: Support for handling physical quantities, unit conversions, etc