quantify 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +105 -3
- metadata +3 -4
- data/Examples.rb +0 -104
data/README
CHANGED
|
@@ -1,7 +1,109 @@
|
|
|
1
1
|
A gem to support for physical quantities and unit conversions
|
|
2
|
-
|
|
3
2
|
Licensed under the MIT license (See COPYING file for details)
|
|
4
|
-
|
|
5
3
|
Author: Andrew Berkeley (andrew.berkeley.is@googlemail.com)
|
|
6
|
-
|
|
7
4
|
Homepage: https://github.com/spatchcock/quantify
|
|
5
|
+
|
|
6
|
+
Examples
|
|
7
|
+
|
|
8
|
+
unit = Unit.km #=> #<Quantify::Unit::SI:0xb75c9718 ... >
|
|
9
|
+
unit.name #=> 'kilometre'
|
|
10
|
+
unit.symbol #=> 'kg'
|
|
11
|
+
unit.dimensions #=> #<Quantify::Dimensions:0xb75c4254 .. >
|
|
12
|
+
unit.measures #=> 'length'
|
|
13
|
+
unit.alternatives :name #=> ['metre', 'megametre', 'gigametre', 'terametre', 'angstrom',
|
|
14
|
+
# 'astronomical unit', 'baromil', 'chain', 'dram', 'ell', 'fathom',
|
|
15
|
+
# 'fermi', 'foot us survey', 'foot', 'furlong', 'hand', 'inch',
|
|
16
|
+
# 'nautical league', 'statute league', 'light year', 'line',
|
|
17
|
+
# 'link', 'yard']
|
|
18
|
+
other_unit = Unit.hour
|
|
19
|
+
other_unit.name #=> 'hour'
|
|
20
|
+
other_unit.symbol #=> 'h'
|
|
21
|
+
other_unit.measures #=> 'time'
|
|
22
|
+
other_unit.alternatives :symbol #=> [ 's', 'ks', 'Ms', 'Gs', 'Ts', 'd', 'min' ]
|
|
23
|
+
|
|
24
|
+
another_unit = unit / other_unit #=> #<Quantify::Unit::Compound:0xb74af323 ... >
|
|
25
|
+
another_unit.name #=> 'kilometer per hour'
|
|
26
|
+
another_unit.symbol #=> 'km h^-1'
|
|
27
|
+
another_unit.measures #=> 'velocity'
|
|
28
|
+
|
|
29
|
+
last_unit = Unit.m
|
|
30
|
+
last.unit.measures #=> 'length'
|
|
31
|
+
square = last_unit ** 2 #=> #<Quantify::Unit::Compound:0xb446f12f ... >
|
|
32
|
+
square.symbol #=> 'm^2'
|
|
33
|
+
square.measures #=> 'area'
|
|
34
|
+
|
|
35
|
+
----
|
|
36
|
+
|
|
37
|
+
Define quantity - method 1
|
|
38
|
+
quantity = Quantity.new(1234.5678, :lb) #=> #<Quantify::Quantity:0xjk39d570 ... >
|
|
39
|
+
quantity.value #=> 1234.5678
|
|
40
|
+
quantity.unit #=> #<Quantify::Unit::NonSI:0xb182124 ... >
|
|
41
|
+
quantity.unit.symbol #=> 'lb'
|
|
42
|
+
|
|
43
|
+
Define quantity - method 2
|
|
44
|
+
string = quantity.to_s #=> "1234.5678 lb"
|
|
45
|
+
quantity = Quantity.parse(string) #=> #<Quantify::Quantity:0xj982b4f9 ... >
|
|
46
|
+
quantity.to_s #=> "1234.5678 lb"
|
|
47
|
+
|
|
48
|
+
Define quantity - method 3
|
|
49
|
+
quantity = 1234.5678.lb #=> #<Quantify::Quantity:02387f7340 ... >
|
|
50
|
+
quantity.to_s #=> "1234.5678 lb"
|
|
51
|
+
|
|
52
|
+
Multiply by scalar
|
|
53
|
+
new_quantity = quantity * 4 #=> #<Quantify::Quantity:87b37f720 ... >
|
|
54
|
+
new_quantity.to_s #=> "4938.2712 lb"
|
|
55
|
+
|
|
56
|
+
Convert unit
|
|
57
|
+
converted_quantity = new_quantity.to_kg #=> #<Quantify::Quantity:0b8787a688 ... >
|
|
58
|
+
converted_quantity.to_s #=> "2239.96213731074 kg"
|
|
59
|
+
|
|
60
|
+
----
|
|
61
|
+
|
|
62
|
+
One line conversion of a quantity to a new unit
|
|
63
|
+
5000 litres into US petroleum barrels
|
|
64
|
+
|
|
65
|
+
5000.L.to_bbl.value #=> 31.4490528488754
|
|
66
|
+
|
|
67
|
+
----
|
|
68
|
+
|
|
69
|
+
Complex example
|
|
70
|
+
|
|
71
|
+
Define energy units
|
|
72
|
+
kW = Unit.kW #=> #<Quantify::Unit::SI:0xb7586620 ... >
|
|
73
|
+
kW.name #=> 'kilowatt'
|
|
74
|
+
kW.measures #=> 'power'
|
|
75
|
+
|
|
76
|
+
h = Unit.h #=> #<Quantify::Unit::NonSI:0xb7582994 ... >
|
|
77
|
+
h.symbol #=> 'h'
|
|
78
|
+
h.measures #=> 'time'
|
|
79
|
+
|
|
80
|
+
Create compound unit for energy
|
|
81
|
+
kWh = kW / h #=> #<Quantify::Unit::Compound:0xb756b0b4 ... >
|
|
82
|
+
kWh.name #=> 'kilowatt hour'
|
|
83
|
+
kWh.measures #=> 'energy'
|
|
84
|
+
kWh.alternatives :symbol #=> ['J', 'kJ', 'MJ', 'GJ', 'TJ', 'BTU', 'cal', 'CHU',
|
|
85
|
+
# 'dyn cm', 'eV', 'erg', 'Eh']
|
|
86
|
+
kWh.alternatives :name #=> ['joule', 'kilojoule', 'megajoule', 'gigajoule',
|
|
87
|
+
# 'terajoule', 'british thermal unit', 'calorie',
|
|
88
|
+
# 'celsius heat unit', 'dyne centimetre', 'electron volt',
|
|
89
|
+
# 'erg', 'hartree']
|
|
90
|
+
|
|
91
|
+
Create mass unit
|
|
92
|
+
kg = Unit.kg #=> #<Quantify::Unit::SI:0xb758b594 ... >
|
|
93
|
+
|
|
94
|
+
Create emission factor compound unit
|
|
95
|
+
kg_per_kWh = kg / kWh #=> #<Quantify::Unit::Compound:0xb746f093 ... >
|
|
96
|
+
kg_per_kWh.symbol #=> "kg_kW^-1_h^-1
|
|
97
|
+
kg_per_kWh.name #=> 'kilogram per kilowatt hour'
|
|
98
|
+
|
|
99
|
+
Create emission factor quantity
|
|
100
|
+
emission_factor = Quantity.new(0.54, kg_per_kWh) #=> #<Quantify::Quantity:0xb75cd570 ... >
|
|
101
|
+
emission_factor.to_s #=> "0.54 kg kW^-1 h^-1"
|
|
102
|
+
|
|
103
|
+
Create consumption quantity
|
|
104
|
+
consumption = Quantity.new(9885.5, kWh) #=> #<Quantify::Quantity:0xb7j4k3570 ... >
|
|
105
|
+
consumption.to_s #=> "9885.5 kWh"
|
|
106
|
+
|
|
107
|
+
Calculate emissions
|
|
108
|
+
emissions = consumption * emission_factor #=> #<Quantify::Quantity:0xb456g2s70 ... >
|
|
109
|
+
emissions.to_s #=> "5338.17 kg"
|
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:
|
|
4
|
+
hash: 21
|
|
5
5
|
prerelease:
|
|
6
6
|
segments:
|
|
7
7
|
- 1
|
|
8
8
|
- 0
|
|
9
|
-
-
|
|
10
|
-
version: 1.0.
|
|
9
|
+
- 1
|
|
10
|
+
version: 1.0.1
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- Andrew Berkeley
|
|
@@ -43,7 +43,6 @@ extra_rdoc_files: []
|
|
|
43
43
|
files:
|
|
44
44
|
- README
|
|
45
45
|
- COPYING
|
|
46
|
-
- Examples.rb
|
|
47
46
|
- lib/quantify.rb
|
|
48
47
|
- lib/quantify/quantify.rb
|
|
49
48
|
- lib/quantify/config.rb
|
data/Examples.rb
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
## Example ##
|
|
2
|
-
|
|
3
|
-
unit = Unit.km #=> #<Quantify::Unit::SI:0xb75c9718 ... >
|
|
4
|
-
unit.name #=> 'kilometre'
|
|
5
|
-
unit.symbol #=> 'kg'
|
|
6
|
-
unit.dimensions #=> #<Quantify::Dimensions:0xb75c4254 .. >
|
|
7
|
-
unit.measures #=> 'length'
|
|
8
|
-
unit.alternatives :name #=> ['metre', 'megametre', 'gigametre', 'terametre', 'angstrom',
|
|
9
|
-
# 'astronomical unit', 'baromil', 'chain', 'dram', 'ell', 'fathom',
|
|
10
|
-
# 'fermi', 'foot us survey', 'foot', 'furlong', 'hand', 'inch',
|
|
11
|
-
# 'nautical league', 'statute league', 'light year', 'line',
|
|
12
|
-
# 'link', 'yard']
|
|
13
|
-
other_unit = Unit.hour
|
|
14
|
-
other_unit.name #=> 'hour'
|
|
15
|
-
other_unit.symbol #=> 'h'
|
|
16
|
-
other_unit.measures #=> 'time'
|
|
17
|
-
other_unit.alternatives :symbol #=> [ 's', 'ks', 'Ms', 'Gs', 'Ts', 'd', 'min' ]
|
|
18
|
-
|
|
19
|
-
another_unit = unit / other_unit #=> #<Quantify::Unit::Compound:0xb74af323 ... >
|
|
20
|
-
another_unit.name #=> 'kilometer per hour'
|
|
21
|
-
another_unit.symbol #=> 'km h^-1'
|
|
22
|
-
another_unit.measures #=> 'velocity'
|
|
23
|
-
|
|
24
|
-
last_unit = Unit.m
|
|
25
|
-
last.unit.measures #=> 'length'
|
|
26
|
-
square = last_unit ** 2 #=> #<Quantify::Unit::Compound:0xb446f12f ... >
|
|
27
|
-
square.symbol #=> 'm^2'
|
|
28
|
-
square.measures #=> 'area'
|
|
29
|
-
|
|
30
|
-
----
|
|
31
|
-
|
|
32
|
-
# Define quantity - method 1
|
|
33
|
-
quantity = Quantity.new(1234.5678, :lb) #=> #<Quantify::Quantity:0xjk39d570 ... >
|
|
34
|
-
quantity.value #=> 1234.5678
|
|
35
|
-
quantity.unit #=> #<Quantify::Unit::NonSI:0xb182124 ... >
|
|
36
|
-
quantity.unit.symbol #=> 'lb'
|
|
37
|
-
|
|
38
|
-
# Define quantity - method 2
|
|
39
|
-
string = quantity.to_s #=> "1234.5678 lb"
|
|
40
|
-
quantity = Quantity.parse(string) #=> #<Quantify::Quantity:0xj982b4f9 ... >
|
|
41
|
-
quantity.to_s #=> "1234.5678 lb"
|
|
42
|
-
|
|
43
|
-
# Define quantity - method 3
|
|
44
|
-
quantity = 1234.5678.lb #=> #<Quantify::Quantity:02387f7340 ... >
|
|
45
|
-
quantity.to_s #=> "1234.5678 lb"
|
|
46
|
-
|
|
47
|
-
# Multiply by scalar
|
|
48
|
-
new_quantity = quantity * 4 #=> #<Quantify::Quantity:87b37f720 ... >
|
|
49
|
-
new_quantity.to_s #=> "4938.2712 lb"
|
|
50
|
-
|
|
51
|
-
# Convert unit
|
|
52
|
-
converted_quantity = new_quantity.to_kg #=> #<Quantify::Quantity:0b8787a688 ... >
|
|
53
|
-
converted_quantity.to_s #=> "2239.96213731074 kg"
|
|
54
|
-
|
|
55
|
-
----
|
|
56
|
-
|
|
57
|
-
# One line conversion of a quantity to a new unit
|
|
58
|
-
# 5000 litres into US petroleum barrels
|
|
59
|
-
|
|
60
|
-
5000.L.to_bbl.value #=> 31.4490528488754
|
|
61
|
-
|
|
62
|
-
----
|
|
63
|
-
|
|
64
|
-
# Complex example
|
|
65
|
-
|
|
66
|
-
# Define energy units
|
|
67
|
-
kW = Unit.kW #=> #<Quantify::Unit::SI:0xb7586620 ... >
|
|
68
|
-
kW.name #=> 'kilowatt'
|
|
69
|
-
kW.measures #=> 'power'
|
|
70
|
-
|
|
71
|
-
h = Unit.h #=> #<Quantify::Unit::NonSI:0xb7582994 ... >
|
|
72
|
-
h.symbol #=> 'h'
|
|
73
|
-
h.measures #=> 'time'
|
|
74
|
-
|
|
75
|
-
# Create compound unit for energy
|
|
76
|
-
kWh = kW / h #=> #<Quantify::Unit::Compound:0xb756b0b4 ... >
|
|
77
|
-
kWh.name #=> 'kilowatt hour'
|
|
78
|
-
kWh.measures #=> 'energy'
|
|
79
|
-
kWh.alternatives :symbol #=> ['J', 'kJ', 'MJ', 'GJ', 'TJ', 'BTU', 'cal', 'CHU',
|
|
80
|
-
# 'dyn cm', 'eV', 'erg', 'Eh']
|
|
81
|
-
kWh.alternative :name #=> ['joule', 'kilojoule', 'megajoule', 'gigajoule',
|
|
82
|
-
# 'terajoule', 'british thermal unit', 'calorie',
|
|
83
|
-
# 'celsius heat unit', 'dyne centimetre', 'electron volt',
|
|
84
|
-
# 'erg', 'hartree']
|
|
85
|
-
|
|
86
|
-
# Create mass unit
|
|
87
|
-
kg = Unit.kg #=> #<Quantify::Unit::SI:0xb758b594 ... >
|
|
88
|
-
|
|
89
|
-
# Create emission factor compound unit
|
|
90
|
-
kg_per_kWh = kg / kWh #=> #<Quantify::Unit::Compound:0xb746f093 ... >
|
|
91
|
-
kg_per_kWh.symbol #=> "kg_kW^-1_h^-1
|
|
92
|
-
kg_per_kWh.name #=> 'kilogram per kilowatt hour'
|
|
93
|
-
|
|
94
|
-
# Create emission factor quantity
|
|
95
|
-
emission_factor = Quantity.new(0.54, kg_per_kWh) #=> #<Quantify::Quantity:0xb75cd570 ... >
|
|
96
|
-
emission_factor.to_s #=> "0.54 kg kW^-1 h^-1"
|
|
97
|
-
|
|
98
|
-
# Create consumption quantity
|
|
99
|
-
consumption = Quantity.new(9885.5, kWh) #=> #<Quantify::Quantity:0xb7j4k3570 ... >
|
|
100
|
-
consumption.to_s #=> "9885.5 kWh"
|
|
101
|
-
|
|
102
|
-
# Calculate emissions
|
|
103
|
-
emissions = consumption * emission_factor #=> #<Quantify::Quantity:0xb456g2s70 ... >
|
|
104
|
-
emissions.to_s #=> "5338.17 kg"
|