unit_measurements 3.8.0 → 4.0.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 +26 -2
- data/Gemfile.lock +1 -1
- data/README.md +68 -80
- data/lib/unit_measurements/arithmetic.rb +4 -4
- data/lib/unit_measurements/base.rb +2 -3
- data/lib/unit_measurements/comparison.rb +2 -1
- data/lib/unit_measurements/conversion.rb +5 -5
- data/lib/unit_measurements/errors/primitive_unit_already_set_error.rb +11 -0
- data/lib/unit_measurements/formatter.rb +2 -2
- data/lib/unit_measurements/math.rb +5 -5
- data/lib/unit_measurements/measurement.rb +5 -11
- data/lib/unit_measurements/normalizer.rb +21 -28
- data/lib/unit_measurements/unit.rb +8 -5
- data/lib/unit_measurements/unit_group.rb +41 -7
- data/lib/unit_measurements/unit_group_builder.rb +10 -27
- data/lib/unit_measurements/unit_groups/acceleration.rb +8 -6
- data/lib/unit_measurements/unit_groups/all.rb +6 -6
- data/lib/unit_measurements/unit_groups/amount_of_substance.rb +4 -4
- data/lib/unit_measurements/unit_groups/angular_acceleration.rb +5 -5
- data/lib/unit_measurements/unit_groups/angular_velocity.rb +14 -14
- data/lib/unit_measurements/unit_groups/area.rb +8 -10
- data/lib/unit_measurements/unit_groups/density.rb +7 -8
- data/lib/unit_measurements/unit_groups/electric_charge.rb +7 -9
- data/lib/unit_measurements/unit_groups/electric_conductance.rb +18 -0
- data/lib/unit_measurements/unit_groups/electric_current.rb +6 -8
- data/lib/unit_measurements/unit_groups/electric_potential.rb +19 -0
- data/lib/unit_measurements/unit_groups/electrical_capacitance.rb +18 -0
- data/lib/unit_measurements/unit_groups/electrical_elastance.rb +13 -0
- data/lib/unit_measurements/unit_groups/electrical_inductance.rb +18 -0
- data/lib/unit_measurements/unit_groups/electrical_resistance.rb +19 -0
- data/lib/unit_measurements/unit_groups/force.rb +9 -11
- data/lib/unit_measurements/unit_groups/length.rb +7 -9
- data/lib/unit_measurements/unit_groups/luminous_intensity.rb +4 -4
- data/lib/unit_measurements/unit_groups/magnetic_field.rb +4 -4
- data/lib/unit_measurements/unit_groups/magnetic_flux.rb +6 -7
- data/lib/unit_measurements/unit_groups/magnetic_induction.rb +4 -6
- data/lib/unit_measurements/unit_groups/plane_angle.rb +11 -10
- data/lib/unit_measurements/unit_groups/quantity.rb +7 -5
- data/lib/unit_measurements/unit_groups/solid_angle.rb +7 -7
- data/lib/unit_measurements/unit_groups/sound_level.rb +4 -4
- data/lib/unit_measurements/unit_groups/temperature.rb +4 -4
- data/lib/unit_measurements/unit_groups/time.rb +13 -12
- data/lib/unit_measurements/unit_groups/velocity.rb +12 -16
- data/lib/unit_measurements/unit_groups/volume.rb +12 -14
- data/lib/unit_measurements/unit_groups/weight.rb +5 -5
- data/lib/unit_measurements/version.rb +1 -1
- data/units.md +72 -72
- metadata +9 -10
- data/lib/unit_measurements/unit_groups/capacitance.rb +0 -19
- data/lib/unit_measurements/unit_groups/conductance.rb +0 -19
- data/lib/unit_measurements/unit_groups/elastance.rb +0 -12
- data/lib/unit_measurements/unit_groups/inductance.rb +0 -19
- data/lib/unit_measurements/unit_groups/resistance.rb +0 -20
- data/lib/unit_measurements/unit_groups/voltage.rb +0 -21
- data/lib/unit_measurements/unit_methods.rb +0 -45
- data/lib/unit_measurements/unit_system.rb +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df02b48d8940d23d2ef2462c58fbdfdd63c5d5d7fbfade71a40a7ddb1508f7b0
|
4
|
+
data.tar.gz: d3773e766bd46d8fdc4c3375bfd195506082d6125aaea66f48dd23891a009ae6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec6a76228693aff998a8ea032a0f8f03732ee137a55ac9a29708b2f346919a5f134463ce117ffefa3374c0b7f2fe13ad5ff699b4f68f7c03703903d2c1b6f94f
|
7
|
+
data.tar.gz: e7de3fa4e74ed1e426b19dcec1e7a08d3f3cebf72b608f841a4a4a95302006a4da4b4b4e8421646f6c1a0345419cb0febe079fd0c033fe02a93f0b4dd498f84a
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,28 @@
|
|
1
|
-
## [
|
1
|
+
## [4.0.0](https://github.com/shivam091/unit_measurements/compare/v3.8.0...v4.0.0) - 2023-10-04
|
2
|
+
|
3
|
+
### What's new
|
4
|
+
|
5
|
+
- Added support to set `primitive` unit for each unit group.
|
6
|
+
- Added support to set `system` in `Unit` metadata.
|
7
|
+
- Added `UnitMeasurements::PrimitiveUnitAlreadySetError` error if `primitive` unit is defined more than one time in the unit group.
|
8
|
+
|
9
|
+
### What's changed
|
10
|
+
|
11
|
+
- Renamed `Voltage` unit group class to `ElectricPotential`.
|
12
|
+
- Renamed `Conductance` unit group class to `ElectricConductance`.
|
13
|
+
- Renamed `Resistance` unit group class to `ElectricalResistance`.
|
14
|
+
- Renamed `Elastance` unit group class to `ElectricalElastance`.
|
15
|
+
- Renamed `Capacitance` unit group class to `ElectricalCapacitance`.
|
16
|
+
- Renamed `Inductance` unit group class to `ElectricalInductance`.
|
17
|
+
|
18
|
+
### What's removed
|
19
|
+
|
20
|
+
- Removed support to build `UnitSystem` instances for each system within the unit group.
|
21
|
+
- Removed support to set `primitive` unit for each unit system within the unit group.
|
22
|
+
|
23
|
+
----------
|
24
|
+
|
25
|
+
## [3.8.0](https://github.com/shivam091/unit_measurements/compare/v3.7.0...v3.8.0) - 2023-10-03
|
2
26
|
|
3
27
|
### What's new
|
4
28
|
|
@@ -7,7 +31,7 @@
|
|
7
31
|
|
8
32
|
----------
|
9
33
|
|
10
|
-
## [3.7.0](https://github.com/shivam091/unit_measurements/compare/v3.6.0...v3.7.0) - 2023-10-
|
34
|
+
## [3.7.0](https://github.com/shivam091/unit_measurements/compare/v3.6.0...v3.7.0) - 2023-10-02
|
11
35
|
|
12
36
|
### What's new
|
13
37
|
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -64,7 +64,7 @@ viz., `UnitMeasurements::Weight`, `UnitMeasurements::Length`, etc.
|
|
64
64
|
**Initialize a measurement:**
|
65
65
|
|
66
66
|
```ruby
|
67
|
-
UnitMeasurements::Length.new(1,
|
67
|
+
UnitMeasurements::Length.new(1, "km")
|
68
68
|
#=> 1 km
|
69
69
|
```
|
70
70
|
|
@@ -76,21 +76,21 @@ This gem allows you to convert among units of same unit group. You can convert m
|
|
76
76
|
You can use `#convert_to` as:
|
77
77
|
|
78
78
|
```ruby
|
79
|
-
UnitMeasurements::Length.new(1,
|
79
|
+
UnitMeasurements::Length.new(1, "km").convert_to("m")
|
80
80
|
#=> 1000.0 m
|
81
81
|
```
|
82
82
|
|
83
83
|
If you want to modify measurement object itself, you can use `#convert_to!` method as:
|
84
84
|
|
85
85
|
```ruby
|
86
|
-
UnitMeasurements::Length.new(1,
|
86
|
+
UnitMeasurements::Length.new(1, "km").convert_to!("m")
|
87
87
|
#=> 1000.0 m
|
88
88
|
```
|
89
89
|
|
90
90
|
You can also chain call of `#convert_to` and `#convert_to!` methods as:
|
91
91
|
|
92
92
|
```ruby
|
93
|
-
UnitMeasurements::Length.new(100,
|
93
|
+
UnitMeasurements::Length.new(100, "m").convert_to("ft").convert_to!("in")
|
94
94
|
#=> 3937.00787401574071916010498688 in
|
95
95
|
```
|
96
96
|
|
@@ -113,11 +113,11 @@ UnitMeasurements::Length.parse("1 km to m")
|
|
113
113
|
**Parse scientific numbers, source unit, and (or) target unit:**
|
114
114
|
|
115
115
|
```ruby
|
116
|
-
UnitMeasurements::Length.new(BigDecimal(2),
|
116
|
+
UnitMeasurements::Length.new(BigDecimal(2), "km").convert_to("m")
|
117
117
|
#=> 20000.0 m
|
118
|
-
UnitMeasurements::Length.new("2e+2",
|
118
|
+
UnitMeasurements::Length.new("2e+2", "km").convert_to("m")
|
119
119
|
#=> 200000.0 m
|
120
|
-
UnitMeasurements::Length.parse("2e² km").convert_to(
|
120
|
+
UnitMeasurements::Length.parse("2e² km").convert_to("m")
|
121
121
|
#=> 200000.0 m
|
122
122
|
UnitMeasurements::Length.parse("2e+2 km to m")
|
123
123
|
#=> 200000.0 m
|
@@ -130,11 +130,11 @@ Supported special characters for exponents are `⁰`, `¹`, `²`, `³`, `⁴`, `
|
|
130
130
|
**Parse complex numbers, source unit, and (or) target unit:**
|
131
131
|
|
132
132
|
```ruby
|
133
|
-
UnitMeasurements::Length.new(Complex(2, 3),
|
133
|
+
UnitMeasurements::Length.new(Complex(2, 3), "km").convert_to("m")
|
134
134
|
#=> 2000.0+3000.0i m
|
135
|
-
UnitMeasurements::Length.new("2+3i",
|
135
|
+
UnitMeasurements::Length.new("2+3i", "km").convert_to("m")
|
136
136
|
#=> 2000.0+3000.0i m
|
137
|
-
UnitMeasurements::Length.parse("2+3i km").convert_to(
|
137
|
+
UnitMeasurements::Length.parse("2+3i km").convert_to("m")
|
138
138
|
#=> 2000.0+3000.0i m
|
139
139
|
UnitMeasurements::Length.parse("2+3i km to m")
|
140
140
|
#=> 2000.0+3000.0i m
|
@@ -143,19 +143,19 @@ UnitMeasurements::Length.parse("2+3i km to m")
|
|
143
143
|
**Parse fractional/mixed fractional numbers, source unit, and (or) target unit:**
|
144
144
|
|
145
145
|
```ruby
|
146
|
-
UnitMeasurements::Length.new(Rational(2, 3),
|
146
|
+
UnitMeasurements::Length.new(Rational(2, 3), "km").convert_to("m")
|
147
147
|
#=> 666.666666666667 m
|
148
|
-
UnitMeasurements::Length.new("2/3",
|
148
|
+
UnitMeasurements::Length.new("2/3", "km").convert_to("m")
|
149
149
|
#=> 666.666666666667 m
|
150
|
-
UnitMeasurements::Length.new("½",
|
150
|
+
UnitMeasurements::Length.new("½", "km").convert_to("m")
|
151
151
|
#=> 500.0 m
|
152
|
-
UnitMeasurements::Length.parse("2 ½ km").convert_to(
|
152
|
+
UnitMeasurements::Length.parse("2 ½ km").convert_to("m")
|
153
153
|
#=> 2500.0 m
|
154
|
-
UnitMeasurements::Length.parse("2/3 km").convert_to(
|
154
|
+
UnitMeasurements::Length.parse("2/3 km").convert_to("m")
|
155
155
|
#=> 666.666666666667 m
|
156
156
|
UnitMeasurements::Length.parse("2/3 km to m")
|
157
157
|
#=> 666.666666666667 m
|
158
|
-
UnitMeasurements::Length.parse("2 1/2 km").convert_to(
|
158
|
+
UnitMeasurements::Length.parse("2 1/2 km").convert_to("m")
|
159
159
|
#=> 2500.0 m
|
160
160
|
UnitMeasurements::Length.parse("2 ½ km to m")
|
161
161
|
#=> 2500.0 m
|
@@ -166,9 +166,9 @@ Supported special characters for fractional notations are `¼`, `½`, `¾`, `⅓
|
|
166
166
|
**Parse ratios, source unit, and (or) target unit:**
|
167
167
|
|
168
168
|
```ruby
|
169
|
-
UnitMeasurements::Length.new("1:2",
|
169
|
+
UnitMeasurements::Length.new("1:2", "km").convert_to("m")
|
170
170
|
#=> 500.0 m
|
171
|
-
UnitMeasurements::Length.parse("1:2 km").convert_to(
|
171
|
+
UnitMeasurements::Length.parse("1:2 km").convert_to("m")
|
172
172
|
#=> 500.0 m
|
173
173
|
UnitMeasurements::Length.parse("1:2 km to m")
|
174
174
|
#=> 500.0 m
|
@@ -180,112 +180,99 @@ If you want to format measurement to certain format, you can use `#format` metho
|
|
180
180
|
If format is not specified, it defaults to `"%.2<value>f %<unit>s"`.
|
181
181
|
|
182
182
|
```ruby
|
183
|
-
UnitMeasurements::Length.new(100,
|
183
|
+
UnitMeasurements::Length.new(100, "m").to("in").format
|
184
184
|
#=> "3937.01 in"
|
185
|
-
UnitMeasurements::Length.new(100,
|
185
|
+
UnitMeasurements::Length.new(100, "m").to("in").format("%.4<quantity>f %<unit>s")
|
186
186
|
#=> "3937.0079 in"
|
187
|
-
UnitMeasurements::Length.new(100,
|
187
|
+
UnitMeasurements::Length.new(100, "m").to("in").format("%.4<quantity>f")
|
188
188
|
#=> "3937.0079"
|
189
189
|
```
|
190
190
|
|
191
191
|
**Extract the unit and the quantity from measurement:**
|
192
192
|
|
193
193
|
```ruby
|
194
|
-
length = UnitMeasurements::Length.new(1,
|
194
|
+
length = UnitMeasurements::Length.new(1, "km")
|
195
195
|
length.quantity
|
196
196
|
#=> 1
|
197
197
|
length.unit
|
198
198
|
#=> #<UnitMeasurements::Unit: km (kilometer, kilometers, kilometre, kilometres)>
|
199
199
|
```
|
200
200
|
|
201
|
-
**See
|
201
|
+
**See primitive unit of the unit group:**
|
202
202
|
|
203
203
|
```ruby
|
204
|
-
UnitMeasurements::Length.
|
205
|
-
#=>
|
206
|
-
```
|
207
|
-
|
208
|
-
**Finding unit system within the unit group:**
|
209
|
-
|
210
|
-
```ruby
|
211
|
-
UnitMeasurements::Length.system_for(:imperial)
|
212
|
-
#=> #<UnitMeasurements::UnitSystem:0x00007f87a348e680
|
213
|
-
# @name=:imperial,
|
214
|
-
# @primitive=#<UnitMeasurements::Unit: in (", inch, inches)>,
|
215
|
-
# @units=[#<UnitMeasurements::Unit: in (", inch, inches)>, #<UnitMeasurements::Unit: ft (', feet, foot)>, ...]>
|
204
|
+
UnitMeasurements::Length.primitive
|
205
|
+
#=> #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
|
216
206
|
```
|
217
207
|
|
218
|
-
**
|
219
|
-
You can use these methods interchangeably between the two classes._**
|
220
|
-
|
221
|
-
**See all units of the unit group/unit system:**
|
208
|
+
**See all units of the unit group:**
|
222
209
|
|
223
210
|
```ruby
|
224
211
|
UnitMeasurements::Length.units
|
225
212
|
#=> [#<UnitMeasurements::Unit: m (meter, meters, metre, metres)>, ..., ...]
|
226
213
|
```
|
227
214
|
|
228
|
-
**See names of all valid units of the unit group
|
215
|
+
**See names of all valid units of the unit group:**
|
229
216
|
|
230
217
|
```ruby
|
231
218
|
UnitMeasurements::Length.unit_names
|
232
219
|
#=> ["m", "km", "mi", "ft", "in", "yd", ...]
|
233
220
|
```
|
234
221
|
|
235
|
-
**See all valid units of the unit group
|
222
|
+
**See all valid units of the unit group along with their aliases:**
|
236
223
|
|
237
224
|
```ruby
|
238
225
|
UnitMeasurements::Length.unit_names_with_aliases
|
239
226
|
#=> ["g", "meter", "metre", "meters", "metres", "km", "kilometer", "kilometre", "kilometers", "kilometres", "in", "inch", "inches", "yd", "yard", "yards", ...]
|
240
227
|
```
|
241
228
|
|
242
|
-
**Finding units within the unit group
|
229
|
+
**Finding units within the unit group:**
|
243
230
|
|
244
231
|
You can use `#unit_for` or `#unit_for!` (aliased as `#[]`) to find units within
|
245
232
|
the unit group. `#unit_for!` method returns error if a unit is not present in the
|
246
233
|
unit group.
|
247
234
|
|
248
235
|
```ruby
|
249
|
-
UnitMeasurements::Length.unit_for(
|
236
|
+
UnitMeasurements::Length.unit_for("m")
|
250
237
|
#=> #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
|
251
|
-
UnitMeasurements::Length.unit_for(
|
238
|
+
UnitMeasurements::Length.unit_for("z")
|
252
239
|
#=> nil
|
253
|
-
UnitMeasurements::Length.unit_for!(
|
240
|
+
UnitMeasurements::Length.unit_for!("m")
|
254
241
|
#=> #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
|
255
|
-
UnitMeasurements::Length.unit_for!(
|
242
|
+
UnitMeasurements::Length.unit_for!("z")
|
256
243
|
#=> Invalid unit: 'z'. (UnitMeasurements::UnitError)
|
257
244
|
```
|
258
245
|
|
259
|
-
**Finding whether the unit is defined within the unit group
|
246
|
+
**Finding whether the unit is defined within the unit group:**
|
260
247
|
|
261
248
|
```ruby
|
262
|
-
UnitMeasurements::Length.defined?(
|
249
|
+
UnitMeasurements::Length.defined?("m")
|
263
250
|
#=> true
|
264
|
-
UnitMeasurements::Length.defined?(
|
251
|
+
UnitMeasurements::Length.defined?("metre")
|
265
252
|
#=> false
|
266
253
|
```
|
267
254
|
|
268
|
-
**Check if the unit is a valid unit or alias within the unit group
|
255
|
+
**Check if the unit is a valid unit or alias within the unit group:**
|
269
256
|
|
270
257
|
```ruby
|
271
|
-
UnitMeasurements::Length.unit_or_alias?(
|
258
|
+
UnitMeasurements::Length.unit_or_alias?("m")
|
272
259
|
#=> true
|
273
|
-
UnitMeasurements::Length.unit_or_alias?(
|
260
|
+
UnitMeasurements::Length.unit_or_alias?("metre")
|
274
261
|
#=> true
|
275
262
|
```
|
276
263
|
|
277
264
|
### Comparisons
|
278
265
|
|
279
266
|
You have ability to compare the measurements with the same or different units within the same unit group.
|
280
|
-
For example, comparing
|
267
|
+
For example, comparing length with length will work, comparing a length with a area would fail.
|
281
268
|
Supported comparisons and methods are `==`, `!=`, `<`, `>`, `<=`, `>=`, `between?`, and `clamp`.
|
282
269
|
|
283
270
|
```ruby
|
284
|
-
UnitMeasurements::Length.new(1, "km") == UnitMeasurements::Length.new(1,
|
271
|
+
UnitMeasurements::Length.new(1, "km") == UnitMeasurements::Length.new(1, "km")
|
285
272
|
#=> true
|
286
273
|
UnitMeasurements::Length.parse("1 km") <= UnitMeasurements::Length.parse("0.5 km")
|
287
274
|
#=> false
|
288
|
-
UnitMeasurements::Length.new(1,
|
275
|
+
UnitMeasurements::Length.new(1, "ft").between?(UnitMeasurements::Length.new(12, "in"), UnitMeasurements::Length.new(24, "in"))
|
289
276
|
#=> true
|
290
277
|
```
|
291
278
|
|
@@ -303,13 +290,13 @@ In cases of different units, the left hand side takes precedence:
|
|
303
290
|
4. `#/` - Divides the measurement quantity by other measurement quantity or number.
|
304
291
|
|
305
292
|
```ruby
|
306
|
-
UnitMeasurements::Length.new(1,
|
293
|
+
UnitMeasurements::Length.new(1, "km") + UnitMeasurements::Length.new(1, "m")
|
307
294
|
#=> 1.001 km
|
308
|
-
UnitMeasurements::Length.new(2,
|
295
|
+
UnitMeasurements::Length.new(2, "km") - 1
|
309
296
|
#=> 1 km
|
310
|
-
UnitMeasurements::Length.new(2,
|
297
|
+
UnitMeasurements::Length.new(2, "km") * 2
|
311
298
|
#=> 4 km
|
312
|
-
UnitMeasurements::Length.new(4,
|
299
|
+
UnitMeasurements::Length.new(4, "km") / UnitMeasurements::Length.new(2, "km")
|
313
300
|
#=> 2 km
|
314
301
|
```
|
315
302
|
|
@@ -324,13 +311,13 @@ You can perform mathematical operations on the measurements.
|
|
324
311
|
4. `#ceil` - Rounds quantity of the measurement to next higher integer.
|
325
312
|
|
326
313
|
```ruby
|
327
|
-
UnitMeasurements::Length.new(1,
|
314
|
+
UnitMeasurements::Length.new(1, "m").to("in").round(4)
|
328
315
|
#=> 39.3701 in
|
329
|
-
UnitMeasurements::Length.new(-17.625,
|
316
|
+
UnitMeasurements::Length.new(-17.625, "m").abs
|
330
317
|
#=> 17.625 m
|
331
|
-
UnitMeasurements::Length.new(17.625,
|
318
|
+
UnitMeasurements::Length.new(17.625, "m").floor
|
332
319
|
#=> 17 m
|
333
|
-
UnitMeasurements::Length.new(17.625,
|
320
|
+
UnitMeasurements::Length.new(17.625, "m").ceil
|
334
321
|
#=> 18 m
|
335
322
|
```
|
336
323
|
|
@@ -340,15 +327,15 @@ You can convert measurement quantity directly to other numeric types viz.
|
|
340
327
|
`Integer`, `BigDecimal`, `Rational`, `Complex`, and `Float`.
|
341
328
|
|
342
329
|
```ruby
|
343
|
-
UnitMeasurements::Length.new(2.25567,
|
330
|
+
UnitMeasurements::Length.new(2.25567, "km").to_i
|
344
331
|
#=> 2 km
|
345
|
-
UnitMeasurements::Length.new(2.25567,
|
332
|
+
UnitMeasurements::Length.new(2.25567, "km").to_f
|
346
333
|
#=> 2.25567 km
|
347
|
-
UnitMeasurements::Length.new(2.25567,
|
334
|
+
UnitMeasurements::Length.new(2.25567, "km").to_r
|
348
335
|
#=> 225567/100000 km
|
349
|
-
UnitMeasurements::Length.new(2.25567,
|
336
|
+
UnitMeasurements::Length.new(2.25567, "km").to_d
|
350
337
|
#=> 2.25567 km
|
351
|
-
UnitMeasurements::Length.new(2.25567,
|
338
|
+
UnitMeasurements::Length.new(2.25567, "km").to_c
|
352
339
|
#=> 2.25567+0i km
|
353
340
|
```
|
354
341
|
|
@@ -356,10 +343,10 @@ UnitMeasurements::Length.new(2.25567, :km).to_c
|
|
356
343
|
|
357
344
|
The **`UnitMeasurements::Unit`** class is used to represent the units for a measurement.
|
358
345
|
|
359
|
-
### SI
|
346
|
+
### Support for SI decimal prefixes
|
360
347
|
|
361
|
-
There is support for SI
|
362
|
-
Units declared through it will have automatic support for all
|
348
|
+
There is support for SI decimal prefixes through the use of `si_unit` method.
|
349
|
+
Units declared through it will have automatic support for all decimal prefixes:
|
363
350
|
|
364
351
|
| Multiplying Factor | SI Prefix | Scientific Notation |
|
365
352
|
| ----------------------------------------- | ---------- | ------------------- |
|
@@ -430,25 +417,26 @@ gem "unit_measurements", require: ["unit_measurements/base", "unit_measurements/
|
|
430
417
|
### Building new unit groups
|
431
418
|
|
432
419
|
This library provides simpler way to build your own unit groups. To build new unit group,
|
433
|
-
use `UnitMeasurements.build` method in order to define units
|
420
|
+
use `UnitMeasurements.build` method in order to define units within it:
|
434
421
|
|
435
|
-
For convenience, you also have ability to group units by the unit system
|
422
|
+
For convenience, you also have ability to group units by the unit system using `system` method
|
423
|
+
and set primitive unit for each unit group using `primitive` method.
|
436
424
|
|
437
425
|
```ruby
|
438
426
|
UnitMeasurements::Time = UnitMeasurements.build do
|
439
|
-
#
|
440
|
-
|
441
|
-
# Set primitive unit for the unit system.
|
442
|
-
primitive :s
|
427
|
+
# Set primitive unit for the unit group.
|
428
|
+
primitive "s"
|
443
429
|
|
430
|
+
# Group units by the unit system.
|
431
|
+
system :metric do
|
444
432
|
# Add a SI unit to the unit group.
|
445
|
-
si_unit
|
433
|
+
si_unit "s", aliases: ["second", "seconds"]
|
446
434
|
|
447
435
|
# Add units to the group, along with their conversion multipliers.
|
448
|
-
unit
|
436
|
+
unit "min", value: "60 s", aliases: ["hour", "hours"]
|
449
437
|
|
450
438
|
# You can also specify unit value as an array.
|
451
|
-
unit :h, value: [60,
|
439
|
+
unit :h, value: [60, "min"], aliases: ["day", "days"]
|
452
440
|
end
|
453
441
|
end
|
454
442
|
```
|
@@ -9,7 +9,7 @@ module UnitMeasurements
|
|
9
9
|
# @param [Numeric or Measurement] other
|
10
10
|
#
|
11
11
|
# @example
|
12
|
-
# UnitMeasurements::Weight.new(1,
|
12
|
+
# UnitMeasurements::Weight.new(1, "kg") + UnitMeasurements::Weight.new(1, "g")
|
13
13
|
# => 1.001 kg
|
14
14
|
#
|
15
15
|
# @return [Measurement]
|
@@ -22,7 +22,7 @@ module UnitMeasurements
|
|
22
22
|
# @param [Numeric or Measurement] other
|
23
23
|
#
|
24
24
|
# @example
|
25
|
-
# UnitMeasurements::Weight.new(2,
|
25
|
+
# UnitMeasurements::Weight.new(2, "kg") - 1
|
26
26
|
# => 1 kg
|
27
27
|
#
|
28
28
|
# @return [Measurement]
|
@@ -35,7 +35,7 @@ module UnitMeasurements
|
|
35
35
|
# @param [Numeric or Measurement] other
|
36
36
|
#
|
37
37
|
# @example
|
38
|
-
# UnitMeasurements::Weight.new(2,
|
38
|
+
# UnitMeasurements::Weight.new(2, "kg") * 2
|
39
39
|
# => 4 kg
|
40
40
|
#
|
41
41
|
# @return [Measurement]
|
@@ -48,7 +48,7 @@ module UnitMeasurements
|
|
48
48
|
# @param [Numeric or Measurement] other
|
49
49
|
#
|
50
50
|
# @example
|
51
|
-
# UnitMeasurements::Weight.new(4,
|
51
|
+
# UnitMeasurements::Weight.new(4, "kg") / UnitMeasurements::Weight.new(2, "kg")
|
52
52
|
# => 2 kg
|
53
53
|
#
|
54
54
|
# @return [Measurement]
|
@@ -24,8 +24,6 @@ end
|
|
24
24
|
|
25
25
|
require "unit_measurements/unit_group_builder"
|
26
26
|
require "unit_measurements/unit"
|
27
|
-
require "unit_measurements/unit_methods"
|
28
|
-
require "unit_measurements/unit_system"
|
29
27
|
require "unit_measurements/unit_group"
|
30
28
|
require "unit_measurements/arithmetic"
|
31
29
|
require "unit_measurements/comparison"
|
@@ -37,5 +35,6 @@ require "unit_measurements/formatter"
|
|
37
35
|
require "unit_measurements/measurement"
|
38
36
|
|
39
37
|
require "unit_measurements/errors/unit_error"
|
40
|
-
require "unit_measurements/errors/unit_already_defined_error"
|
41
38
|
require "unit_measurements/errors/parse_error"
|
39
|
+
require "unit_measurements/errors/unit_already_defined_error"
|
40
|
+
require "unit_measurements/errors/primitive_unit_already_set_error"
|
@@ -6,7 +6,8 @@ module UnitMeasurements
|
|
6
6
|
module Comparison
|
7
7
|
include Comparable
|
8
8
|
|
9
|
-
#
|
9
|
+
# Implements +Comparable+ methods on the quantities of two measurements of
|
10
|
+
# the same unit group.
|
10
11
|
#
|
11
12
|
# @example
|
12
13
|
# UnitMeasurements::Weight.new(1, "kg") == UnitMeasurements::Weight.new(1, :kg)
|
@@ -7,7 +7,7 @@ module UnitMeasurements
|
|
7
7
|
# Converts quantity of the measurement to +Integer+.
|
8
8
|
#
|
9
9
|
# @example
|
10
|
-
# UnitMeasurements::Weight.new(2.25567,
|
10
|
+
# UnitMeasurements::Weight.new(2.25567, "kg").to_i
|
11
11
|
# => 2 kg
|
12
12
|
#
|
13
13
|
# @return [Measurement]
|
@@ -18,7 +18,7 @@ module UnitMeasurements
|
|
18
18
|
# Converts quantity of the measurement to +Float+.
|
19
19
|
#
|
20
20
|
# @example
|
21
|
-
# UnitMeasurements::Weight.new(2.25567,
|
21
|
+
# UnitMeasurements::Weight.new(2.25567, "kg").to_f
|
22
22
|
# => 2.25567 kg
|
23
23
|
#
|
24
24
|
# @return [Measurement]
|
@@ -29,7 +29,7 @@ module UnitMeasurements
|
|
29
29
|
# Converts quantity of the measurement to +Rational+.
|
30
30
|
#
|
31
31
|
# @example
|
32
|
-
# UnitMeasurements::Weight.new(2.25567,
|
32
|
+
# UnitMeasurements::Weight.new(2.25567, "kg").to_r
|
33
33
|
# => 225567/100000 kg
|
34
34
|
#
|
35
35
|
# @return [Measurement]
|
@@ -40,7 +40,7 @@ module UnitMeasurements
|
|
40
40
|
# Converts quantity of the measurement to +Complex+.
|
41
41
|
#
|
42
42
|
# @example
|
43
|
-
# UnitMeasurements::Weight.new(2.25567,
|
43
|
+
# UnitMeasurements::Weight.new(2.25567, "kg").to_c
|
44
44
|
# => 2.25567+0i kg
|
45
45
|
#
|
46
46
|
# @return [Measurement]
|
@@ -51,7 +51,7 @@ module UnitMeasurements
|
|
51
51
|
# Converts quantity of the measurement to +BigDecimal+.
|
52
52
|
#
|
53
53
|
# @example
|
54
|
-
# UnitMeasurements::Weight.new(2.25567,
|
54
|
+
# UnitMeasurements::Weight.new(2.25567, "kg").to_d
|
55
55
|
# => 2.25567 kg
|
56
56
|
#
|
57
57
|
# @return [Measurement]
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
module UnitMeasurements
|
6
|
+
class PrimitiveUnitAlreadySetError < BaseError
|
7
|
+
def initialize
|
8
|
+
super("The primitive unit is already set for the unit group.")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -12,9 +12,9 @@ module UnitMeasurements
|
|
12
12
|
# formatting the measurement
|
13
13
|
#
|
14
14
|
# @example
|
15
|
-
# UnitMeasurements::Weight.parse("2 kg").to(
|
15
|
+
# UnitMeasurements::Weight.parse("2 kg").to("st").format
|
16
16
|
# => "0.31 st"
|
17
|
-
# UnitMeasurements::Weight.parse("2 kg").to(
|
17
|
+
# UnitMeasurements::Weight.parse("2 kg").to("st").format("%.4<quantity>f %<unit>s")
|
18
18
|
# => "0.3149 st"
|
19
19
|
#
|
20
20
|
# @param [String] format
|
@@ -5,10 +5,10 @@
|
|
5
5
|
module UnitMeasurements
|
6
6
|
module Math
|
7
7
|
# Rounds quantity of the measurement. If `ndigits` is not specified,
|
8
|
-
# quantity is rounded to
|
8
|
+
# quantity is rounded to +Integer+.
|
9
9
|
#
|
10
10
|
# @example
|
11
|
-
# UnitMeasurements::Weight.new(1,
|
11
|
+
# UnitMeasurements::Weight.new(1, "g").convert_to("st").round(4)
|
12
12
|
# => 0.0002 st
|
13
13
|
#
|
14
14
|
# @param [Integer] ndigits
|
@@ -21,7 +21,7 @@ module UnitMeasurements
|
|
21
21
|
# Returns absolute value of the measurement quantity.
|
22
22
|
#
|
23
23
|
# @example
|
24
|
-
# UnitMeasurements::Length.new(-17.625,
|
24
|
+
# UnitMeasurements::Length.new(-17.625, "m").abs
|
25
25
|
# => 17.625 m
|
26
26
|
#
|
27
27
|
# @return [Measurement]
|
@@ -32,7 +32,7 @@ module UnitMeasurements
|
|
32
32
|
# Rounds quantity of the measurement to next lower integer.
|
33
33
|
#
|
34
34
|
# @example
|
35
|
-
# UnitMeasurements::Length.new(17.625,
|
35
|
+
# UnitMeasurements::Length.new(17.625, "m").floor
|
36
36
|
# => 17 m
|
37
37
|
#
|
38
38
|
# @return [Measurement]
|
@@ -43,7 +43,7 @@ module UnitMeasurements
|
|
43
43
|
# Rounds quantity of the measurement to next higher integer.
|
44
44
|
#
|
45
45
|
# @example
|
46
|
-
# UnitMeasurements::Length.new(17.625,
|
46
|
+
# UnitMeasurements::Length.new(17.625, "m").ceil
|
47
47
|
# => 18 m
|
48
48
|
#
|
49
49
|
# @return [Measurement]
|
@@ -31,9 +31,7 @@ module UnitMeasurements
|
|
31
31
|
|
32
32
|
self.class.new((quantity * conversion_factor), target_unit)
|
33
33
|
end
|
34
|
-
|
35
|
-
alias_method :in, :convert_to
|
36
|
-
alias_method :as, :convert_to
|
34
|
+
[:to, :in, :as].each { |method_alias| alias_method method_alias, :convert_to }
|
37
35
|
|
38
36
|
def convert_to!(target_unit)
|
39
37
|
measurement = convert_to(target_unit)
|
@@ -41,14 +39,10 @@ module UnitMeasurements
|
|
41
39
|
|
42
40
|
self
|
43
41
|
end
|
44
|
-
|
45
|
-
alias_method :in!, :convert_to!
|
46
|
-
alias_method :as!, :convert_to!
|
42
|
+
[:to!, :in!, :as!].each { |method_alias| alias_method method_alias, :convert_to! }
|
47
43
|
|
48
44
|
def inspect(dump: false)
|
49
|
-
|
50
|
-
|
51
|
-
to_s
|
45
|
+
dump ? super() : to_s
|
52
46
|
end
|
53
47
|
|
54
48
|
def to_s
|
@@ -71,9 +65,9 @@ module UnitMeasurements
|
|
71
65
|
raise BaseError, "`Measurement` does not have a `unit_group` object. You cannot directly use `Measurement`. Instead, build a new unit group by calling `UnitMeasurements.build`."
|
72
66
|
end
|
73
67
|
|
74
|
-
def_delegators :unit_group, :units, :unit_names, :unit_with_name_and_aliases,
|
68
|
+
def_delegators :unit_group, :primitive, :units, :unit_names, :unit_with_name_and_aliases,
|
75
69
|
:unit_names_with_aliases, :unit_for, :unit_for!, :defined?,
|
76
|
-
:unit_or_alias?, :[]
|
70
|
+
:unit_or_alias?, :[]
|
77
71
|
|
78
72
|
def parse(input)
|
79
73
|
input = Normalizer.normalize(input)
|