unit_measurements 3.4.0 → 3.5.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +22 -1
- data/Gemfile.lock +1 -1
- data/README.md +123 -210
- data/lib/unit_measurements/measurement.rb +0 -4
- data/lib/unit_measurements/unit_groups/all.rb +2 -0
- data/lib/unit_measurements/unit_groups/capacitance.rb +19 -0
- data/lib/unit_measurements/unit_groups/elastance.rb +12 -0
- data/lib/unit_measurements/version.rb +1 -1
- data/units.md +20 -0
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4435eefb4098bb9a5419dd0d712b670996649aedf3c325c12a0e3d23ab6f1b35
|
|
4
|
+
data.tar.gz: cd4806a5b68e37e39cd7473ce2020a98137c8848e59a95a05f905cef39e54835
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6ec86d1da972eaabdf90f2df5e58a37973250ad0d6a3b7d5d8c3445529f4b289f720f06041757bb325d18b49c39c00a70a83f1466f78d842de8583077befbc1e
|
|
7
|
+
data.tar.gz: ead67e5a9f286591e3f2b6a79032122128963f5deb3186beaa27c5c586ba4492acf6ea881ffc574e8d7a133ab3c771593554b4540878b331164bab55b3572241
|
data/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,25 @@
|
|
|
1
|
-
## [3.
|
|
1
|
+
## [3.5.0](https://github.com/shivam091/unit_measurements/compare/v3.4.1...v3.5.0) - 2023-08-30
|
|
2
|
+
|
|
3
|
+
### What's new
|
|
4
|
+
|
|
5
|
+
- Added unit group for `capacitance` units.
|
|
6
|
+
- Added unit group for `elastance` units.
|
|
7
|
+
|
|
8
|
+
----------
|
|
9
|
+
|
|
10
|
+
## [3.4.1](https://github.com/shivam091/unit_measurements/compare/v3.4.0...v3.4.1) - 2023-08-29
|
|
11
|
+
|
|
12
|
+
### What's changed
|
|
13
|
+
|
|
14
|
+
- Updated usage examples in readme.
|
|
15
|
+
|
|
16
|
+
### What's removed
|
|
17
|
+
|
|
18
|
+
- Removed overridden `Measurement.name` method.
|
|
19
|
+
|
|
20
|
+
----------
|
|
21
|
+
|
|
22
|
+
## [3.4.0](https://github.com/shivam091/unit_measurements/compare/v3.3.0...v3.4.0) - 2023-08-28
|
|
2
23
|
|
|
3
24
|
### What's new
|
|
4
25
|
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -24,10 +24,10 @@ The `unit_measurements` gem is designed to simplify the handling of units for sc
|
|
|
24
24
|
|
|
25
25
|
## Advantages
|
|
26
26
|
|
|
27
|
-
1.
|
|
28
|
-
2.
|
|
29
|
-
|
|
30
|
-
4.
|
|
27
|
+
1. It provides easy conversion between units.
|
|
28
|
+
2. It is lightweight and easily extensible to include other units and conversions.
|
|
29
|
+
2. It has built in support for various [unit groups](units.md).
|
|
30
|
+
4. It can convert `complex`, `fractional`, `mixed fractional`, `scientific` numbers, and `ratios`.
|
|
31
31
|
|
|
32
32
|
## Disclaimer
|
|
33
33
|
|
|
@@ -57,205 +57,152 @@ Or otherwise simply install it yourself as:
|
|
|
57
57
|
|
|
58
58
|
## Usage
|
|
59
59
|
|
|
60
|
-
The **`UnitMeasurements::Measurement`** class is responsible for conversion of quantity to various
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
but rather with the unit group classes viz., `UnitMeasurements::Weight`, `UnitMeasurements::Length`, etc.
|
|
60
|
+
The **`UnitMeasurements::Measurement`** class is responsible for conversion of quantity to various compatible units
|
|
61
|
+
but it can't be directly initialized or converted to other units, but rather it is done with the unit group classes
|
|
62
|
+
viz., `UnitMeasurements::Weight`, `UnitMeasurements::Length`, etc.
|
|
64
63
|
|
|
65
64
|
**Initialize a measurement:**
|
|
66
65
|
|
|
67
66
|
```ruby
|
|
68
|
-
UnitMeasurements::
|
|
69
|
-
#=> 1
|
|
67
|
+
UnitMeasurements::Length.new(1, :km)
|
|
68
|
+
#=> 1 km
|
|
70
69
|
```
|
|
71
70
|
|
|
72
71
|
**Converting to other units:**
|
|
73
72
|
|
|
74
|
-
This gem allows you to convert among units of same unit group.
|
|
75
|
-
|
|
76
|
-
(aliased as `#to`, `#in`, and `as`) or `#convert_to!`
|
|
77
|
-
(aliased as `#to!`, `#in!`, and `as!`) methods.
|
|
73
|
+
This gem allows you to convert among units of same unit group. You can convert measurement to other unit using `#convert_to`
|
|
74
|
+
(aliased as `#to`, `#in`, and `#as`) or `#convert_to!` (aliased as `#to!`, `#in!`, and `#as!`) methods.
|
|
78
75
|
|
|
79
76
|
You can use `#convert_to` as:
|
|
80
77
|
|
|
81
78
|
```ruby
|
|
82
|
-
UnitMeasurements::
|
|
83
|
-
#=> 1000.0
|
|
79
|
+
UnitMeasurements::Length.new(1, :km).convert_to(:m)
|
|
80
|
+
#=> 1000.0 m
|
|
84
81
|
```
|
|
85
82
|
|
|
86
83
|
If you want to modify measurement object itself, you can use `#convert_to!` method as:
|
|
87
84
|
|
|
88
85
|
```ruby
|
|
89
|
-
UnitMeasurements::
|
|
90
|
-
#=> 1000.0
|
|
86
|
+
UnitMeasurements::Length.new(1, :km).convert_to!(:m)
|
|
87
|
+
#=> 1000.0 m
|
|
91
88
|
```
|
|
92
89
|
|
|
93
90
|
You can also chain call of `#convert_to` and `#convert_to!` methods as:
|
|
94
91
|
|
|
95
92
|
```ruby
|
|
96
|
-
UnitMeasurements::
|
|
97
|
-
#=>
|
|
93
|
+
UnitMeasurements::Length.new(100, :m).convert_to(:ft).convert_to!(:in)
|
|
94
|
+
#=> 3937.00787401574071916010498688 in
|
|
98
95
|
```
|
|
99
96
|
|
|
100
97
|
**Parse string without having to split out the quantity and source unit:**
|
|
101
98
|
|
|
102
99
|
```ruby
|
|
103
|
-
UnitMeasurements::
|
|
104
|
-
#=> 1.0
|
|
100
|
+
UnitMeasurements::Length.parse("1 km")
|
|
101
|
+
#=> 1.0 km
|
|
105
102
|
```
|
|
106
103
|
|
|
107
104
|
**Parse string that mentions quantity, source unit, and target unit:**
|
|
108
105
|
|
|
109
|
-
|
|
110
|
-
UnitMeasurements::Weight.parse("1 kg to g")
|
|
111
|
-
#=> 1000.0 g
|
|
112
|
-
UnitMeasurements::Weight.parse("1 kg as g")
|
|
113
|
-
#=> 1000.0 g
|
|
114
|
-
UnitMeasurements::Weight.parse("1 kg in g")
|
|
115
|
-
#=> 1000.0 g
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
**Parse rational numbers, source unit, and (or) target unit:**
|
|
119
|
-
|
|
120
|
-
```ruby
|
|
121
|
-
UnitMeasurements::Weight.new(Rational(2, 3), :kg).convert_to(:g)
|
|
122
|
-
#=> 666.666666666667 g
|
|
123
|
-
UnitMeasurements::Weight.new("2/3", :kg).convert_to(:g)
|
|
124
|
-
#=> 666.666666666667 g
|
|
125
|
-
UnitMeasurements::Weight.parse("2/3 kg").convert_to(:g)
|
|
126
|
-
#=> 666.666666666667 g
|
|
127
|
-
UnitMeasurements::Weight.parse("2/3 kg to g")
|
|
128
|
-
#=> 666.666666666667 g
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
**Parse complex numbers, source unit, and (or) target unit:**
|
|
106
|
+
A source unit can be separated from the target unit using the `in`, `to`, or `as` operators.
|
|
132
107
|
|
|
133
108
|
```ruby
|
|
134
|
-
UnitMeasurements::
|
|
135
|
-
#=>
|
|
136
|
-
UnitMeasurements::Weight.new("2+3i", :kg).convert_to(:g)
|
|
137
|
-
#=> 2000.0+3000.0i g
|
|
138
|
-
UnitMeasurements::Weight.parse("2+3i kg").convert_to(:g)
|
|
139
|
-
#=> 2000.0+3000.0i g
|
|
140
|
-
UnitMeasurements::Weight.parse("2+3i kg to g")
|
|
141
|
-
#=> 2000.0+3000.0i g
|
|
109
|
+
UnitMeasurements::Length.parse("1 km to m")
|
|
110
|
+
#=> 1000.0 m
|
|
142
111
|
```
|
|
143
112
|
|
|
144
113
|
**Parse scientific numbers, source unit, and (or) target unit:**
|
|
145
114
|
|
|
146
115
|
```ruby
|
|
147
|
-
UnitMeasurements::
|
|
148
|
-
#=>
|
|
149
|
-
UnitMeasurements::
|
|
150
|
-
#=>
|
|
151
|
-
UnitMeasurements::
|
|
152
|
-
#=>
|
|
153
|
-
UnitMeasurements::
|
|
154
|
-
#=>
|
|
116
|
+
UnitMeasurements::Length.new(BigDecimal(2), :km).convert_to(:m)
|
|
117
|
+
#=> 20000.0 m
|
|
118
|
+
UnitMeasurements::Length.new("2e+2", :km).convert_to(:m)
|
|
119
|
+
#=> 200000.0 m
|
|
120
|
+
UnitMeasurements::Length.parse("2e² km").convert_to(:m)
|
|
121
|
+
#=> 200000.0 m
|
|
122
|
+
UnitMeasurements::Length.parse("2e+2 km to m")
|
|
123
|
+
#=> 200000.0 m
|
|
124
|
+
UnitMeasurements::Length.parse("2e⁻² km to m")
|
|
125
|
+
#=> 20.0 m
|
|
155
126
|
```
|
|
156
127
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
```ruby
|
|
160
|
-
UnitMeasurements::Weight.new("1:2", :kg).convert_to(:g)
|
|
161
|
-
#=> 500.0 g
|
|
162
|
-
UnitMeasurements::Weight.parse("1:2 kg").convert_to(:g)
|
|
163
|
-
#=> 500.0 g
|
|
164
|
-
UnitMeasurements::Weight.parse("1:2 kg to g")
|
|
165
|
-
#=> 500.0 g
|
|
166
|
-
```
|
|
128
|
+
Supported special characters for exponents are `⁰`, `¹`, `²`, `³`, `⁴`, `⁵`, `⁶`, `⁷`, `⁸`, `⁹`, `⁺`, `⁻`.
|
|
167
129
|
|
|
168
|
-
**Parse
|
|
130
|
+
**Parse complex numbers, source unit, and (or) target unit:**
|
|
169
131
|
|
|
170
132
|
```ruby
|
|
171
|
-
UnitMeasurements::
|
|
172
|
-
#=>
|
|
173
|
-
UnitMeasurements::
|
|
174
|
-
#=>
|
|
175
|
-
UnitMeasurements::
|
|
176
|
-
#=>
|
|
177
|
-
UnitMeasurements::
|
|
178
|
-
#=>
|
|
179
|
-
UnitMeasurements::Weight.parse("½ kg").convert_to(:g)
|
|
180
|
-
#=> 500.0 g
|
|
181
|
-
UnitMeasurements::Weight.parse("½ kg to g")
|
|
182
|
-
#=> 500.0 g
|
|
133
|
+
UnitMeasurements::Length.new(Complex(2, 3), :km).convert_to(:m)
|
|
134
|
+
#=> 2000.0+3000.0i m
|
|
135
|
+
UnitMeasurements::Length.new("2+3i", :km).convert_to(:m)
|
|
136
|
+
#=> 2000.0+3000.0i m
|
|
137
|
+
UnitMeasurements::Length.parse("2+3i km").convert_to(:m)
|
|
138
|
+
#=> 2000.0+3000.0i m
|
|
139
|
+
UnitMeasurements::Length.parse("2+3i km to m")
|
|
140
|
+
#=> 2000.0+3000.0i m
|
|
183
141
|
```
|
|
184
142
|
|
|
185
|
-
**Parse mixed fractional
|
|
143
|
+
**Parse fractional/mixed fractional numbers, source unit, and (or) target unit:**
|
|
186
144
|
|
|
187
145
|
```ruby
|
|
188
|
-
UnitMeasurements::
|
|
189
|
-
#=>
|
|
190
|
-
UnitMeasurements::
|
|
191
|
-
#=>
|
|
192
|
-
UnitMeasurements::
|
|
193
|
-
#=>
|
|
194
|
-
UnitMeasurements::
|
|
195
|
-
#=> 2500.0
|
|
196
|
-
UnitMeasurements::
|
|
197
|
-
#=>
|
|
198
|
-
UnitMeasurements::
|
|
199
|
-
#=>
|
|
146
|
+
UnitMeasurements::Length.new(Rational(2, 3), :km).convert_to(:m)
|
|
147
|
+
#=> 666.666666666667 m
|
|
148
|
+
UnitMeasurements::Length.new("2/3", :km).convert_to(:m)
|
|
149
|
+
#=> 666.666666666667 m
|
|
150
|
+
UnitMeasurements::Length.new("½", :km).convert_to(:m)
|
|
151
|
+
#=> 500.0 m
|
|
152
|
+
UnitMeasurements::Length.parse("2 ½ km").convert_to(:m)
|
|
153
|
+
#=> 2500.0 m
|
|
154
|
+
UnitMeasurements::Length.parse("2/3 km").convert_to(:m)
|
|
155
|
+
#=> 666.666666666667 m
|
|
156
|
+
UnitMeasurements::Length.parse("2/3 km to m")
|
|
157
|
+
#=> 666.666666666667 m
|
|
158
|
+
UnitMeasurements::Length.parse("2 1/2 km").convert_to(:m)
|
|
159
|
+
#=> 2500.0 m
|
|
160
|
+
UnitMeasurements::Length.parse("2 ½ km to m")
|
|
161
|
+
#=> 2500.0 m
|
|
200
162
|
```
|
|
201
163
|
|
|
202
164
|
Supported special characters for fractional notations are `¼`, `½`, `¾`, `⅓`, `⅔`, `⅕`, `⅖`, `⅗`, `⅘`, `⅙`, `⅚`, `⅐`, `⅛`, `⅜`, `⅝`, `⅞`, `⅑`, `⅒`, `↉`, `⁄`.
|
|
203
165
|
|
|
204
|
-
**Parse
|
|
166
|
+
**Parse ratios, source unit, and (or) target unit:**
|
|
205
167
|
|
|
206
168
|
```ruby
|
|
207
|
-
UnitMeasurements::
|
|
208
|
-
#=>
|
|
209
|
-
UnitMeasurements::
|
|
210
|
-
#=>
|
|
211
|
-
UnitMeasurements::
|
|
212
|
-
#=>
|
|
213
|
-
UnitMeasurements::Weight.new("2e⁺²", :kg).convert_to(:g)
|
|
214
|
-
#=> 200000.0 g
|
|
215
|
-
UnitMeasurements::Weight.parse("2e⁺2 kg").convert_to(:g)
|
|
216
|
-
#=> 200000.0 g
|
|
217
|
-
UnitMeasurements::Weight.parse("2e⁻² kg to g")
|
|
218
|
-
#=> 20.0 g
|
|
169
|
+
UnitMeasurements::Length.new("1:2", :km).convert_to(:m)
|
|
170
|
+
#=> 500.0 m
|
|
171
|
+
UnitMeasurements::Length.parse("1:2 km").convert_to(:m)
|
|
172
|
+
#=> 500.0 m
|
|
173
|
+
UnitMeasurements::Length.parse("1:2 km to m")
|
|
174
|
+
#=> 500.0 m
|
|
219
175
|
```
|
|
220
176
|
|
|
221
|
-
Supported special characters for exponents are `⁰`, `¹`, `²`, `³`, `⁴`, `⁵`, `⁶`, `⁷`, `⁸`, `⁹`, `⁺`, `⁻`.
|
|
222
|
-
|
|
223
177
|
**Formatting measurement:**
|
|
224
178
|
|
|
225
179
|
If you want to format measurement to certain format, you can use `#format` method.
|
|
226
180
|
If format is not specified, it defaults to `"%.2<value>f %<unit>s"`.
|
|
227
181
|
|
|
228
182
|
```ruby
|
|
229
|
-
UnitMeasurements::
|
|
230
|
-
#=> "
|
|
231
|
-
UnitMeasurements::
|
|
232
|
-
#=> "
|
|
233
|
-
UnitMeasurements::
|
|
234
|
-
#=> "
|
|
183
|
+
UnitMeasurements::Length.new(100, :m).to(:in).format
|
|
184
|
+
#=> "3937.01 in"
|
|
185
|
+
UnitMeasurements::Length.new(100, :m).to(:in).format("%.4<quantity>f %<unit>s")
|
|
186
|
+
#=> "3937.0079 in"
|
|
187
|
+
UnitMeasurements::Length.new(100, :m).to(:in).format("%.4<quantity>f")
|
|
188
|
+
#=> "3937.0079"
|
|
235
189
|
```
|
|
236
190
|
|
|
237
191
|
**Extract the unit and the quantity from measurement:**
|
|
238
192
|
|
|
239
193
|
```ruby
|
|
240
|
-
|
|
241
|
-
|
|
194
|
+
length = UnitMeasurements::Length.new(1, :km)
|
|
195
|
+
length.quantity
|
|
242
196
|
#=> 1
|
|
243
|
-
|
|
244
|
-
#=> #<UnitMeasurements::Unit:
|
|
245
|
-
```
|
|
246
|
-
|
|
247
|
-
**See humanized name of the unit group:**
|
|
248
|
-
|
|
249
|
-
```ruby
|
|
250
|
-
UnitMeasurements::Weight.name
|
|
251
|
-
#=> weight
|
|
197
|
+
length.unit
|
|
198
|
+
#=> #<UnitMeasurements::Unit: km (kilometer, kilometers, kilometre, kilometres)>
|
|
252
199
|
```
|
|
253
200
|
|
|
254
201
|
**See all unit systems defined in the unit group:**
|
|
255
202
|
|
|
256
203
|
```ruby
|
|
257
204
|
UnitMeasurements::Length.systems
|
|
258
|
-
#=> [#<UnitMeasurements::UnitSystem:0x00007fa1a46d11c0 @name=:metric, @primitive=#<UnitMeasurements::Unit: m (meter, meters, metre, metres)>, @units=[]
|
|
205
|
+
#=> [#<UnitMeasurements::UnitSystem:0x00007fa1a46d11c0 @name=:metric, @primitive=#<UnitMeasurements::Unit: m (meter, meters, metre, metres)>, @units=[]>, ...]
|
|
259
206
|
```
|
|
260
207
|
|
|
261
208
|
**Finding unit system within the unit group:**
|
|
@@ -268,31 +215,28 @@ UnitMeasurements::Length.system_for(:imperial)
|
|
|
268
215
|
# @units=[#<UnitMeasurements::Unit: in (", inch, inches)>, #<UnitMeasurements::Unit: ft (', feet, foot)>, ...]>
|
|
269
216
|
```
|
|
270
217
|
|
|
218
|
+
**_Note: The `UnitMeasurements::UnitGroup` and `UnitMeasurements::UnitSystem` instances share the same set of methods for unit handling.
|
|
219
|
+
You can use these methods interchangeably between the two classes._**
|
|
220
|
+
|
|
271
221
|
**See all units of the unit group/unit system:**
|
|
272
222
|
|
|
273
223
|
```ruby
|
|
274
|
-
UnitMeasurements::
|
|
275
|
-
#=> [#<UnitMeasurements::Unit:
|
|
276
|
-
UnitMeasurements::Length.system_for(:imperial).units
|
|
277
|
-
#=> [#<UnitMeasurements::Unit: in (", inch, inches)>, #<UnitMeasurements::Unit: ft (', feet, foot)>, ...]
|
|
224
|
+
UnitMeasurements::Length.units
|
|
225
|
+
#=> [#<UnitMeasurements::Unit: m (meter, meters, metre, metres)>, ..., ...]
|
|
278
226
|
```
|
|
279
227
|
|
|
280
228
|
**See names of all valid units of the unit group/unit system:**
|
|
281
229
|
|
|
282
230
|
```ruby
|
|
283
|
-
UnitMeasurements::
|
|
284
|
-
#=> ["
|
|
285
|
-
UnitMeasurements::Length.system_for(:imperial).unit_names
|
|
286
|
-
#=> ["ft", "in", "mi", "yd"]
|
|
231
|
+
UnitMeasurements::Length.unit_names
|
|
232
|
+
#=> ["m", "km", "mi", "ft", "in", "yd", ...]
|
|
287
233
|
```
|
|
288
234
|
|
|
289
235
|
**See all valid units of the unit group/unit system along with their aliases:**
|
|
290
236
|
|
|
291
237
|
```ruby
|
|
292
|
-
UnitMeasurements::
|
|
293
|
-
#=> ["g", "
|
|
294
|
-
UnitMeasurements::Length.system_for(:imperial).unit_names_with_aliases
|
|
295
|
-
#=> ["feet", "foot", "ft", "in", "inch", "inches", "mi", "mile", "miles", "yard", "yards", "yd"]
|
|
238
|
+
UnitMeasurements::Length.unit_names_with_aliases
|
|
239
|
+
#=> ["g", "meter", "metre", "meters", "metres", "km", "kilometer", "kilometre", "kilometers", "kilometres", "in", "inch", "inches", "yd", "yard", "yards", ...]
|
|
296
240
|
```
|
|
297
241
|
|
|
298
242
|
**Finding units within the unit group/unit system:**
|
|
@@ -302,50 +246,31 @@ the unit group. `#unit_for!` method returns error if a unit is not present in th
|
|
|
302
246
|
unit group.
|
|
303
247
|
|
|
304
248
|
```ruby
|
|
305
|
-
UnitMeasurements::
|
|
306
|
-
#=> #<UnitMeasurements::Unit:
|
|
307
|
-
UnitMeasurements::
|
|
249
|
+
UnitMeasurements::Length.unit_for(:m)
|
|
250
|
+
#=> #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
|
|
251
|
+
UnitMeasurements::Length.unit_for(:z)
|
|
308
252
|
#=> nil
|
|
309
|
-
UnitMeasurements::
|
|
310
|
-
#=> #<UnitMeasurements::Unit:
|
|
311
|
-
UnitMeasurements::
|
|
253
|
+
UnitMeasurements::Length.unit_for!(:m)
|
|
254
|
+
#=> #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
|
|
255
|
+
UnitMeasurements::Length.unit_for!(:z)
|
|
312
256
|
#=> Invalid unit: 'z'. (UnitMeasurements::UnitError)
|
|
313
|
-
|
|
314
|
-
UnitMeasurements::Length.system_for(:imperial).unit_for(:in)
|
|
315
|
-
#=> #<UnitMeasurements::Unit: in (", inch, inches)>
|
|
316
|
-
UnitMeasurements::Length.system_for(:imperial).unit_for(:m)
|
|
317
|
-
#=> nil
|
|
318
|
-
UnitMeasurements::Length.system_for(:imperial).unit_for!(:in)
|
|
319
|
-
#=> #<UnitMeasurements::Unit: in (", inch, inches)>
|
|
320
|
-
UnitMeasurements::Length.system_for(:imperial).unit_for!(:m)
|
|
321
|
-
#=> Invalid unit: 'm'. (UnitMeasurements::UnitError)
|
|
322
257
|
```
|
|
323
258
|
|
|
324
259
|
**Finding whether the unit is defined within the unit group/unit system:**
|
|
325
260
|
|
|
326
261
|
```ruby
|
|
327
|
-
UnitMeasurements::
|
|
262
|
+
UnitMeasurements::Length.defined?(:m)
|
|
328
263
|
#=> true
|
|
329
|
-
UnitMeasurements::
|
|
330
|
-
#=> false
|
|
331
|
-
|
|
332
|
-
UnitMeasurements::Length.system_for(:metric).defined?(:m)
|
|
333
|
-
#=> true
|
|
334
|
-
UnitMeasurements::Length.system_for(:metric).defined?(:in)
|
|
264
|
+
UnitMeasurements::Length.defined?(:metre)
|
|
335
265
|
#=> false
|
|
336
266
|
```
|
|
337
267
|
|
|
338
268
|
**Check if the unit is a valid unit or alias within the unit group/unit system:**
|
|
339
269
|
|
|
340
270
|
```ruby
|
|
341
|
-
UnitMeasurements::
|
|
342
|
-
#=> true
|
|
343
|
-
UnitMeasurements::Weight.unit_or_alias?(:gramme)
|
|
344
|
-
#=> true
|
|
345
|
-
|
|
346
|
-
UnitMeasurements::Length.system_for(:metric).unit_or_alias?(:m)
|
|
271
|
+
UnitMeasurements::Length.unit_or_alias?(:m)
|
|
347
272
|
#=> true
|
|
348
|
-
UnitMeasurements::Length.
|
|
273
|
+
UnitMeasurements::Length.unit_or_alias?(:metre)
|
|
349
274
|
#=> true
|
|
350
275
|
```
|
|
351
276
|
|
|
@@ -353,29 +278,22 @@ UnitMeasurements::Length.system_for(:metric).unit_or_alias?(:metre)
|
|
|
353
278
|
|
|
354
279
|
You have ability to compare the measurements with the same or different units within the same unit group.
|
|
355
280
|
For example, comparing weight with weight will work, comparing a weight with a area would fail.
|
|
281
|
+
Supported comparisons and methods are `==`, `!=`, `<`, `>`, `<=`, `>=`, `between?`, and `clamp`.
|
|
356
282
|
|
|
357
283
|
```ruby
|
|
358
|
-
UnitMeasurements::
|
|
284
|
+
UnitMeasurements::Length.new(1, "km") == UnitMeasurements::Length.new(1, :km)
|
|
359
285
|
#=> true
|
|
360
|
-
UnitMeasurements::
|
|
361
|
-
#=> true
|
|
362
|
-
UnitMeasurements::Weight.parse("1 kg") != UnitMeasurements::Weight.parse("1 g")
|
|
363
|
-
#=> true
|
|
364
|
-
UnitMeasurements::Weight.parse("1 kg") <= UnitMeasurements::Weight.parse("0.5 kg")
|
|
286
|
+
UnitMeasurements::Length.parse("1 km") <= UnitMeasurements::Length.parse("0.5 km")
|
|
365
287
|
#=> false
|
|
366
|
-
UnitMeasurements::Weight.parse("1 kg") >= UnitMeasurements::Weight.parse("0.5 kg")
|
|
367
|
-
#=> true
|
|
368
288
|
UnitMeasurements::Length.new(1, :ft).between?(UnitMeasurements::Length.new(12, :in), UnitMeasurements::Length.new(24, :in))
|
|
369
289
|
#=> true
|
|
370
|
-
UnitMeasurements::Length.new(1, :ft).clamp(UnitMeasurements::Length.new(13, :in), UnitMeasurements::Length.new(24, :in))
|
|
371
|
-
#=> 13 in
|
|
372
290
|
```
|
|
373
291
|
|
|
374
292
|
### Arithmetic
|
|
375
293
|
|
|
376
294
|
You have ability to perform arithmetic operations on measurements with the same or
|
|
377
295
|
different units within a same unit group. You can perform arithmetic operations on
|
|
378
|
-
measurement by either other compatible
|
|
296
|
+
measurement by either other measurement with compatible unit or number.
|
|
379
297
|
In cases of different units, the left hand side takes precedence:
|
|
380
298
|
|
|
381
299
|
**Methods:**
|
|
@@ -385,14 +303,14 @@ In cases of different units, the left hand side takes precedence:
|
|
|
385
303
|
4. `#/` - Divides the measurement quantity by other measurement quantity or number.
|
|
386
304
|
|
|
387
305
|
```ruby
|
|
388
|
-
UnitMeasurements::
|
|
389
|
-
#=> 1.001
|
|
390
|
-
UnitMeasurements::
|
|
391
|
-
#=> 1
|
|
392
|
-
UnitMeasurements::
|
|
393
|
-
#=> 4
|
|
394
|
-
UnitMeasurements::
|
|
395
|
-
#=> 2
|
|
306
|
+
UnitMeasurements::Length.new(1, :km) + UnitMeasurements::Length.new(1, :m)
|
|
307
|
+
#=> 1.001 km
|
|
308
|
+
UnitMeasurements::Length.new(2, :km) - 1
|
|
309
|
+
#=> 1 km
|
|
310
|
+
UnitMeasurements::Length.new(2, :km) * 2
|
|
311
|
+
#=> 4 km
|
|
312
|
+
UnitMeasurements::Length.new(4, :km) / UnitMeasurements::Length.new(2, :km)
|
|
313
|
+
#=> 2 km
|
|
396
314
|
```
|
|
397
315
|
|
|
398
316
|
### Math
|
|
@@ -406,8 +324,8 @@ You can perform mathematical operations on the measurements.
|
|
|
406
324
|
4. `#ceil` - Rounds quantity of the measurement to next higher integer.
|
|
407
325
|
|
|
408
326
|
```ruby
|
|
409
|
-
UnitMeasurements::
|
|
410
|
-
#=>
|
|
327
|
+
UnitMeasurements::Length.new(1, :m).to(:in).round(4)
|
|
328
|
+
#=> 39.3701 in
|
|
411
329
|
UnitMeasurements::Length.new(-17.625, :m).abs
|
|
412
330
|
#=> 17.625 m
|
|
413
331
|
UnitMeasurements::Length.new(17.625, :m).floor
|
|
@@ -422,16 +340,16 @@ You can convert measurement quantity directly to other numeric types viz.
|
|
|
422
340
|
`Integer`, `BigDecimal`, `Rational`, `Complex`, and `Float`.
|
|
423
341
|
|
|
424
342
|
```ruby
|
|
425
|
-
UnitMeasurements::
|
|
426
|
-
#=> 2
|
|
427
|
-
UnitMeasurements::
|
|
428
|
-
#=> 2.25567
|
|
429
|
-
UnitMeasurements::
|
|
430
|
-
#=> 225567/100000
|
|
431
|
-
UnitMeasurements::
|
|
432
|
-
#=> 2.25567
|
|
433
|
-
UnitMeasurements::
|
|
434
|
-
#=> 2.25567+0i
|
|
343
|
+
UnitMeasurements::Length.new(2.25567, :km).to_i
|
|
344
|
+
#=> 2 km
|
|
345
|
+
UnitMeasurements::Length.new(2.25567, :km).to_f
|
|
346
|
+
#=> 2.25567 km
|
|
347
|
+
UnitMeasurements::Length.new(2.25567, :km).to_r
|
|
348
|
+
#=> 225567/100000 km
|
|
349
|
+
UnitMeasurements::Length.new(2.25567, :km).to_d
|
|
350
|
+
#=> 2.25567 km
|
|
351
|
+
UnitMeasurements::Length.new(2.25567, :km).to_c
|
|
352
|
+
#=> 2.25567+0i km
|
|
435
353
|
```
|
|
436
354
|
|
|
437
355
|
## Units
|
|
@@ -501,8 +419,6 @@ gem "unit_measurements", require: "unit_measurements/base"
|
|
|
501
419
|
require "unit_measurements/base"
|
|
502
420
|
|
|
503
421
|
require "unit_measurements/unit_groups/length"
|
|
504
|
-
require "unit_measurements/unit_groups/weight"
|
|
505
|
-
require "unit_measurements/unit_groups/volume"
|
|
506
422
|
```
|
|
507
423
|
|
|
508
424
|
or
|
|
@@ -514,10 +430,7 @@ gem "unit_measurements", require: ["unit_measurements/base", "unit_measurements/
|
|
|
514
430
|
### Building new unit groups
|
|
515
431
|
|
|
516
432
|
This library provides simpler way to build your own unit groups. To build new unit group,
|
|
517
|
-
use `UnitMeasurements.build` in order to define units within it:
|
|
518
|
-
|
|
519
|
-
If the unit is supporting [si prefixes](#si-units-support), you can use `si_unit` method to build it.
|
|
520
|
-
If you build unit using `si_unit`, the unit will be added along with all SI prefixes for it.
|
|
433
|
+
use `UnitMeasurements.build` method in order to define units and unit systems within it:
|
|
521
434
|
|
|
522
435
|
For convenience, you also have ability to group units by the unit system and set primitive unit for each unit system using `system` and `primitive` methods.
|
|
523
436
|
|
|
@@ -528,7 +441,7 @@ UnitMeasurements::Time = UnitMeasurements.build do
|
|
|
528
441
|
# Set primitive unit for the unit system.
|
|
529
442
|
primitive :s
|
|
530
443
|
|
|
531
|
-
# Add a SI unit to the unit group
|
|
444
|
+
# Add a SI unit to the unit group.
|
|
532
445
|
si_unit :s, aliases: [:second, :seconds]
|
|
533
446
|
|
|
534
447
|
# Add units to the group, along with their conversion multipliers.
|
|
@@ -540,7 +453,7 @@ UnitMeasurements::Time = UnitMeasurements.build do
|
|
|
540
453
|
end
|
|
541
454
|
```
|
|
542
455
|
|
|
543
|
-
All units allow aliases, as long as they are unique. Unit
|
|
456
|
+
All units allow aliases, as long as they are unique. Unit name can be used to
|
|
544
457
|
define the unit as long as it is unique. All unit names are case sensitive.
|
|
545
458
|
|
|
546
459
|
### Namespaces
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
# -*- frozen_string_literal: true -*-
|
|
3
|
+
# -*- warn_indent: true -*-
|
|
4
|
+
|
|
5
|
+
UnitMeasurements::Capacitance = UnitMeasurements.build do
|
|
6
|
+
system :metric do
|
|
7
|
+
primitive :F
|
|
8
|
+
|
|
9
|
+
si_unit :F, aliases: [:farad, :farads]
|
|
10
|
+
unit :"C/V", value: "1 F", aliases: [:"C·V⁻¹", :"coulomb/volt", :"coulombs/volt", :"coulomb per volt", :"coulombs per volt"]
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
system :centimetre_gram_second do
|
|
14
|
+
primitive :statF
|
|
15
|
+
|
|
16
|
+
unit :abF, value: "1e+9 F", aliases: [:abfarad, :abfarads]
|
|
17
|
+
unit :statF, value: "1.1127e-12 F", aliases: [:statfarad, :statfarads]
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
# -*- frozen_string_literal: true -*-
|
|
3
|
+
# -*- warn_indent: true -*-
|
|
4
|
+
|
|
5
|
+
UnitMeasurements::Elastance = UnitMeasurements.build do
|
|
6
|
+
system :metric do
|
|
7
|
+
primitive :D
|
|
8
|
+
|
|
9
|
+
si_unit :D, aliases: [:"F⁻¹", :daraf, :darafs, :"reciprocal farad", :"reciprocal farads"]
|
|
10
|
+
unit :"V/C", value: "1 D", aliases: [:"V·C⁻¹", :"volt/coulomb", :"volts/coulomb", :"volt per coulomb", :"volts per coulomb"]
|
|
11
|
+
end
|
|
12
|
+
end
|
data/units.md
CHANGED
|
@@ -271,3 +271,23 @@ These units are defined in `UnitMeasurements::ElectricCharge`.
|
|
|
271
271
|
| 3 | statC | statcoulomb, statcoulombs |
|
|
272
272
|
| 4 | Fr | franklin, franklins |
|
|
273
273
|
| 5 | Fd | faraday, faradays |
|
|
274
|
+
|
|
275
|
+
## 22. Capacitance
|
|
276
|
+
|
|
277
|
+
These units are defined in `UnitMeasurements::Capacitance`.
|
|
278
|
+
|
|
279
|
+
| # | Symbol | Aliases |
|
|
280
|
+
|:--|:--|:--|
|
|
281
|
+
| 1 | F* | farad, farads |
|
|
282
|
+
| 2 | abF | abfarad, abfarads |
|
|
283
|
+
| 3 | statF | statfarad, statfarads |
|
|
284
|
+
| 4 | C/V | C·V⁻¹, coulomb/volt, coulombs/volt, coulomb per volt, coulombs per volt |
|
|
285
|
+
|
|
286
|
+
## 23. Elastance
|
|
287
|
+
|
|
288
|
+
These units are defined in `UnitMeasurements::Elastance`.
|
|
289
|
+
|
|
290
|
+
| # | Name | Aliases |
|
|
291
|
+
|:--|:--|:--|
|
|
292
|
+
| 1 | D* | F⁻¹, daraf, darafs, reciprocal farad, reciprocal farads |
|
|
293
|
+
| 2 | V/C | V·C⁻¹, volt/coulomb, volts/coulomb, volt per coulomb, volts per coulomb |
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: unit_measurements
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Harshal LADHE
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2023-09-
|
|
11
|
+
date: 2023-09-30 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|
|
@@ -124,7 +124,9 @@ files:
|
|
|
124
124
|
- lib/unit_measurements/unit_groups/angular_acceleration.rb
|
|
125
125
|
- lib/unit_measurements/unit_groups/angular_velocity.rb
|
|
126
126
|
- lib/unit_measurements/unit_groups/area.rb
|
|
127
|
+
- lib/unit_measurements/unit_groups/capacitance.rb
|
|
127
128
|
- lib/unit_measurements/unit_groups/density.rb
|
|
129
|
+
- lib/unit_measurements/unit_groups/elastance.rb
|
|
128
130
|
- lib/unit_measurements/unit_groups/electric_charge.rb
|
|
129
131
|
- lib/unit_measurements/unit_groups/electric_current.rb
|
|
130
132
|
- lib/unit_measurements/unit_groups/force.rb
|