sy 2.0.21 → 2.0.22
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/README.md +42 -24
- data/lib/sy.rb +7 -0
- data/lib/sy/version.rb +1 -1
- data/test/sy_test.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea2f55f3a2deb6fb8766eff1c6e72d10085e761f
|
4
|
+
data.tar.gz: 01725b3cb13367650ca24a01037bf00214454cf1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13ea62aae24ced86d136b1a62f561321f7ae05c26d38aff7b0cd2dbbfcc01f5a7f8346caa3bcab67de6452bf3608ccfadd612d64957821bfb1457ae3dabebfc5
|
7
|
+
data.tar.gz: 3a471bda9369293a74d3e056663b32efd4b1d68725b585f6b212468123b7d2296d3352e3980c2fbf866c20f3cfc220da319955f70f797952f9a93eaa491d8526
|
data/README.md
CHANGED
@@ -1,15 +1,21 @@
|
|
1
1
|
# SY - The physical units library.
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
`SY` is a domain model of physical units. It can be used in two modes:
|
4
|
+
|
5
|
+
* When loaded by `require 'sy'`, Numeric class is extended with methods
|
6
|
+
corresponding to units and their abbreviations.
|
7
|
+
* When loaded by `require 'sy/noinclude'`, built-in classes are not modified,
|
8
|
+
while physical magnitudes can still be contructed explicitly from the
|
9
|
+
appropriate physical quantities.
|
10
|
+
|
11
|
+
At this place, good manners require me to mention the other good library of
|
12
|
+
physical units in Ruby, [phys-units](https://github.com/masa16/phys-units).
|
7
13
|
|
8
14
|
## Usage
|
9
15
|
|
10
|
-
Upon `require 'sy'`, we can say `5.metre`, or `Rational( 5, 2 ).metre`, and
|
11
|
-
|
12
|
-
|
16
|
+
Upon `require 'sy'`, we can say `5.metre`, or `Rational( 5, 2 ).metre`, and the
|
17
|
+
computer will understand, that these numbers represent magnitudes of the
|
18
|
+
physical quantity `SY::Length` expressed in the unit `SY::METRE`. Equally,
|
13
19
|
we can use abbreviations (such as `5.m`, `2.5.m`), prefixes (such as `5.km`,
|
14
20
|
`5.kilometre`, `5.km`), exponents (such as `5.m²` for 5 square metres), and
|
15
21
|
chaining (such as `5.m.s⁻¹` to denote speed of 5 metres per second). Please
|
@@ -18,26 +24,38 @@ quantities and their units.
|
|
18
24
|
|
19
25
|
## Unicode exponents
|
20
26
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
revisions.
|
27
|
+
Users of `sy` should learn how to type Unicode exponent characters, such as `²`,
|
28
|
+
`³`, `⁻¹` etc. It is possible to use alterantive syntax, such as `5.m.s(-1)`
|
29
|
+
instead of `5.m.s⁻¹`, but Unicode exponents should be used everywere except
|
30
|
+
non-reused code. Unicode exponents make the physical models that you will be
|
31
|
+
constructing with SY much more readable. And we know that code is (typically)
|
32
|
+
write once, read many times. So it pays off to type an extra keystroke when
|
33
|
+
writing to increase readability for the many subsequent revisions.
|
29
34
|
|
30
35
|
## Method collisions
|
31
36
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
37
|
+
If `sy` is used in the mode that extends Numeric with unit methods, then since
|
38
|
+
many of these are short and common words, there can be collisions. For example,
|
39
|
+
`ActiveSupport` already provides handling for time units (hour, minute, second
|
40
|
+
etc.), which would collide with SY methods of the same name. Since `SY` relies
|
41
|
+
on `method_missing`, if these methods are already defined for numerics, `SY`'s
|
42
|
+
method_missing will not activate and ActiveSupport methods will be used. In this
|
43
|
+
particular case, `SY` methods still can be invoked using abbreviations (`5.s`,
|
44
|
+
`5.h`, `5.min`).
|
45
|
+
|
46
|
+
## Noinclude mode
|
47
|
+
|
48
|
+
When _noinclude_ mode is activated by calling `require 'sy/noinclude'`, methods
|
49
|
+
such as `5.metre` cannot be used. Instead, the magnitude has to be constructed
|
50
|
+
explicitly:
|
51
|
+
```ruby
|
52
|
+
mgn = SY::Length.magnitude( 5 )
|
53
|
+
```
|
54
|
+
Another effect of _noinclude_ mode is, that one cannot perform conversions using
|
55
|
+
unit symbols, as in `SY::WATTHOUR.in( :J )`, but instead one has to write:
|
56
|
+
```ruby
|
57
|
+
SY::WATTHOUR.in( SY::JOULE )
|
58
|
+
```
|
41
59
|
|
42
60
|
## Contributing
|
43
61
|
|
data/lib/sy.rb
CHANGED
@@ -294,6 +294,9 @@ module SY
|
|
294
294
|
# make SY::WATT its standard unit:
|
295
295
|
WATT = Unit.standard of: Power, short: "W"
|
296
296
|
|
297
|
+
# Watthour (Wh) is a common unit of energy.
|
298
|
+
WATTHOUR = Unit.of Energy, short: "Wh", amount: WATT * HOUR
|
299
|
+
|
297
300
|
# SY::Pressure...
|
298
301
|
Pressure = ( Force / Area ).standard!
|
299
302
|
|
@@ -328,4 +331,8 @@ module SY
|
|
328
331
|
|
329
332
|
# Having defined Joules and Kelvins, we can spell out the Boltzmann constant:
|
330
333
|
Kʙ = BOLTZMANN_CONSTANT = 1.380648813e-23 * JOULE / KELVIN
|
334
|
+
|
335
|
+
ELEMENTARY_CHARGE = 1.60217656535e-19 * COULOMB
|
336
|
+
|
337
|
+
ELECTRONVOLT = Unit.of Energy, short: "eV", amount: ELEMENTARY_CHARGE * VOLT
|
331
338
|
end
|
data/lib/sy/version.rb
CHANGED
data/test/sy_test.rb
CHANGED
@@ -441,9 +441,9 @@ describe SY::Magnitude do
|
|
441
441
|
assert_equal [SY::LitreVolume], SY::Volume.coerces
|
442
442
|
assert b.quantity.absolute.coerces?( a.quantity.absolute )
|
443
443
|
assert b.quantity.coerces?( a.quantity )
|
444
|
-
assert_equal 0, 1.l <=> 1.
|
444
|
+
assert_equal 0, 1.l <=> 1.l
|
445
445
|
assert_equal -1, 1.m³ <=> 11.hl
|
446
446
|
assert_equal 1, 1.m³ <=> 9.hl
|
447
|
-
assert_equal 1.dm³, 1.
|
447
|
+
assert_equal 1.dm³, 1.dm³
|
448
448
|
end
|
449
449
|
end
|