quantify 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README +105 -31
- data/lib/quantify/unit/compound_unit.rb +1 -1
- metadata +3 -3
data/README
CHANGED
@@ -3,6 +3,83 @@ Licensed under the MIT license (See COPYING file for details)
|
|
3
3
|
Author: Andrew Berkeley (andrew.berkeley.is@googlemail.com)
|
4
4
|
Homepage: https://github.com/spatchcock/quantify
|
5
5
|
|
6
|
+
Quick introduction
|
7
|
+
|
8
|
+
# Quantity operations
|
9
|
+
|
10
|
+
12.feet + 12.feet => "24.0 feet"
|
11
|
+
|
12
|
+
6.m ** 2 => "36.0 m^2"
|
13
|
+
|
14
|
+
100.km / 2.h => "50 kilometers per hour"
|
15
|
+
|
16
|
+
5000.L.to_bbl => "31.4490528488754 barrels"
|
17
|
+
|
18
|
+
1.5.lb.to_si.round(2) => "0.68 kg"
|
19
|
+
|
20
|
+
Unit.ratio :kg, :ton => "1.016047 kilograms per long ton"
|
21
|
+
|
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.
|
25
|
+
|
26
|
+
# Handling units
|
27
|
+
|
28
|
+
Unit.ton.name => "long ton"
|
29
|
+
|
30
|
+
Unit.ton.label => "ton_uk"
|
31
|
+
|
32
|
+
Unit.ton.measures => "mass"
|
33
|
+
|
34
|
+
Unit.ton.alternatives :name => [ "kilogram",
|
35
|
+
"gram",
|
36
|
+
"carat",
|
37
|
+
"electron mass",
|
38
|
+
"grain",
|
39
|
+
"hundredweight long",
|
40
|
+
"hundredweight short",
|
41
|
+
"ounce",
|
42
|
+
"pennyweight",
|
43
|
+
"pound",
|
44
|
+
"short ton",
|
45
|
+
"stone",
|
46
|
+
"tonne",
|
47
|
+
"unified atomic mass" ]
|
48
|
+
|
49
|
+
Unit.ton.si_unit => 'kg'
|
50
|
+
|
51
|
+
Unit.ton.dimensions => <Quantify::Dimensions:0xb75467c8 ... >
|
52
|
+
|
53
|
+
Unit.ton.dimensions.describe => "mass"
|
54
|
+
|
55
|
+
Unit.ton.dimensions.mass => 1
|
56
|
+
|
57
|
+
Unit.ton.dimensions.length => nil
|
58
|
+
|
59
|
+
|
60
|
+
General introduction
|
61
|
+
|
62
|
+
Quantify represents physical quantities using the Quantify::Quantity class.
|
63
|
+
|
64
|
+
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.
|
82
|
+
|
6
83
|
There are several ways to initialize a quantity object
|
7
84
|
|
8
85
|
mass = Quantity.new(100,:lb) => <Quantify::Quantity:0xb7332bbc ... >
|
@@ -30,62 +107,57 @@ Quantity object attributes
|
|
30
107
|
|
31
108
|
Convert a quantity to a different unit
|
32
109
|
|
33
|
-
energy = 100.kWh =>
|
34
|
-
energy.to_s(:name) => "100 kilowatt hours"
|
110
|
+
energy = 100.kWh => "100 kilowatt hours"
|
35
111
|
|
36
|
-
|
37
|
-
energy.to_s => "360.0 MJ"
|
112
|
+
new_energy = energy.to_megajoules => "360.0 MJ"
|
38
113
|
|
39
|
-
|
40
|
-
energy.to_s => "360.0 MJ"
|
114
|
+
new_energy = energy.to_MJ => "360.0 MJ"
|
41
115
|
|
42
|
-
|
43
|
-
energy.to_s => "360.0 MJ"
|
116
|
+
new_energy = energy.to(:MJ) => "360.0 MJ"
|
44
117
|
|
45
118
|
# Initialize a unit object and pass as conversion argument
|
46
119
|
unit = Unit.MJ => <Quantify::Unit::SI:0xb75c9718 ... >
|
47
|
-
|
48
|
-
energy.to_s => "360.0 MJ"
|
120
|
+
new_energy = energy.to(unit) => "360.0 MJ"
|
49
121
|
|
50
|
-
#
|
51
|
-
|
122
|
+
# Note: all of the above results are string representations of the actual
|
123
|
+
# objects which result from these operations.
|
52
124
|
|
53
125
|
Convert the units of a quantity with a compound unit
|
54
126
|
|
55
|
-
speed = 70.mi/1.h =>
|
56
|
-
speed.to_s => "70.0 mi h^-1"
|
127
|
+
speed = 70.mi/1.h => "70.0 mi h^-1"
|
57
128
|
|
58
|
-
speed_in_kms = speed.to_km =>
|
59
|
-
speed_in_kms.to_s => "112.65408 km h^-1"
|
129
|
+
speed_in_kms = speed.to_km => "112.65408 km h^-1"
|
60
130
|
|
61
|
-
speed_in_mins = speed_in_kms.to_min =>
|
62
|
-
|
131
|
+
speed_in_mins = speed_in_kms.to_min => "1.877568 km min^-1"
|
132
|
+
|
133
|
+
# Note: all of the above results are string representations of the actual
|
134
|
+
# objects which result from these operations.
|
63
135
|
|
64
136
|
Convert a quantity to the corresponding SI unit
|
65
137
|
|
66
|
-
energy = 100.kWh =>
|
67
|
-
|
68
|
-
|
69
|
-
|
138
|
+
energy = 100.kWh => "100 kWh"
|
139
|
+
si = quantity.to_si => "360000000.0 J"
|
140
|
+
|
141
|
+
# Note: all of the above results are string representations of the actual
|
142
|
+
# objects which result from these operations.
|
70
143
|
|
71
144
|
Operate on a quantity
|
72
145
|
|
73
|
-
mass = 10.kg * 3 =>
|
74
|
-
mass.to_s => 30.0 kg
|
146
|
+
mass = 10.kg * 3 => "30.0 kg"
|
75
147
|
|
76
|
-
distance = 100.light_years / 20 =>
|
77
|
-
distance.to_s => 5.0 ly
|
148
|
+
distance = 100.light_years / 20 => "5.0 ly"
|
78
149
|
|
79
|
-
area = 10.m * 10.m =>
|
80
|
-
area.to_s :name => 100.0 square metres
|
150
|
+
area = 10.m * 10.m => "100.0 square metres"
|
81
151
|
|
82
|
-
speed = 250.mi / 3.h =>
|
83
|
-
speed.to_s(:name) => 83.3333333333333 miles per hour
|
152
|
+
speed = 250.mi / 3.h => "83.3333333333333 miles per hour"
|
84
153
|
|
85
154
|
speed = 70.mi/1.h => <Quantify::Quantity:0xb7332bbc ... >
|
86
155
|
time = 0.5.h => <Quantify::Quantity:3xf3472hjc ... >
|
87
156
|
distance = speed * time => <Quantify::Quantity:7d7f8g9d5g ... >
|
88
|
-
distance.to_s => 35.0 mi
|
157
|
+
distance.to_s => "35.0 mi"
|
158
|
+
|
159
|
+
# Note: all of the above results are string representations of the actual
|
160
|
+
# objects which result from these operations.
|
89
161
|
|
90
162
|
Additional operations
|
91
163
|
The result of quantity operations is commonly a new quantity with a compound unit.
|
@@ -165,3 +237,5 @@ Initialize a unit object
|
|
165
237
|
square = last_unit ** 2 => <Quantify::Unit::Compound:0xb446f12f ... >
|
166
238
|
square.symbol => 'm^2'
|
167
239
|
square.measures => 'area'
|
240
|
+
|
241
|
+
|
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: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 4
|
10
|
+
version: 1.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andrew Berkeley
|