module SY
The most prominent feature of SY
is, that it extends the Numeric
class with methods corresponding to units and their abbreviations.
In other words, we can say 5.metre, or Rational( 5, 2 ).metre, and the computer will understand, that these numbers represent magnitudes of the physical quantity SY::Length
expressed in the unit SY::METRE
. Equally, we can use abbreviations (such as 5.m, 2.5.m), prefixes (such as 5.km, 5.kilometre, 5.km), exponents (such as 5.m² for 5 square metres) and chaining (such as 5.m.s⁻¹ to denote speed of 5 metres per second).
You should definitely learn how to type Unicode exponent characters, such as ², ³, ⁻¹ etc. It is possible to use alterantive syntax such as 5.m.s(-1) instead of 5.m.s⁻¹, but you should avoid it whenever possible. Unicode exponents make the physical models that you will be constructing with SY
much more readable. And we know that code is (usually) write once, read many times. So it pays off to type an extra keystroke when writing the to make the model more readable for the many subsequent revisions.
One more remark here would be, that due to the fact, that many unit names and abbreviations are very short and common words, there can be collisions. For example ActiveSupport already provides handling for time units (hour, minute, second etc.), which would collide with SY
methods of the same name. Since SY
relies on method_missing, if these methods are already defined for numerics, SY
method_missing will not activate and ActiveSupport methods will be used. In this particular case, SY
methods still can be invoked using abbreviations (5.s, 5.h, 5.min)
Instead of introduction¶ ↑
SY
module defines certain usual constants, quantities and units. The best introduction to how SY
works would be if we take a look at the examples of the most common quantities and units right here in the SY
module:
Here, fixed assets of the main module are set up.
Imperial units.
Setting AUTOINCLUDE
constant to false at the beginning of the sy
loading process prevents automatic inclusion of SY::ExpressibleInUnits
in Numeric
.
Constants
- ACRE
Area
¶ ↑- AMPERE
make
SY::AMPERE
its standard unit:- AUTOINCLUDE
- AVOGADRO_CONSTANT
- Acceleration
Similar for SY::Acceleration:
- Amount
Let
SY::Amount
be a standard dimensionless quantity:- Area
Quantity
SY::Area
is obtained by raising quantitySY::Length
to 2:- BASE_DIMENSIONS
- BOLTZMANN_CONSTANT
- CALORIE
SY::CALORIE
means thermochemical calorie:- CELSIUS
Degree celsius is
SY::CELSIUS
- CELSIUS_MEASURE
CELSIUS_MEASURE
is an object ofSY::Measure
class, that captures the conversion between centigrades and Kelvins.- COULOMB
And
SY::COULOMB
be its standard unit:- CelsiusTemperature
Celsius temperature is a little bit peculiar in that it has offset of 273.15.K with respect to Kelvin temperature, and I am not sure whether at this moment
SY
is handling this right. But nevertheless:- DALTON
And
SY::DALTON
another…- DAY
SY::DAY
is defined in accordance with SI as 86_400.second- DimensionError
- ELECTRONVOLT
- ELEMENTARY_CHARGE
- ElectricCharge
Let
SY::ElectricCharge
be a standard quantity of basic dimension Q:- ElectricCurrent
- ElectricPotential
- Energy
For
SY::Energy
…- FATHOM
- FIRKIN
- FOOT
short: 'in' would be ambiguous
- FORTNIGHT
Time
¶ ↑- FURLONG
forget CHAIN
- Force
For
SY::Force
…- Frequency
SY::Frequency
, a quantity that many will expect:- GALLON
- GRAM
Let
SY::GRAM
be another unit ofSY::Mass
, equal to 0.001.kg:- HERTZ
SY::HERTZ
is its unit:- HOUR
And
SY::HOUR
another:- HUMAN_BODY_TEMPERATURE
- IMPERIAL_TON
- INCH
Length
¶ ↑- JOULE
Make
SY::JOULE
its standard unit:- KELVIN
And
SY::KELVIN
be its standard unit:- KILOGRAM
Let
SY::KILOGRAM
be its standard unit:- LIGHTYEAR
Supplementary unit of length.
- LITRE
SY::LITRE
is the standard unit of SY::LitreVolume:- Length
Let
SY::Length
be a standard quantity of basic dimension L:- LitreVolume
SY::LitreVolume
is another quantity of the same dimension as SY::Volume:- METRE
Let
SY::METRE
be its standard unit.- MILE
- MINUTE
Let
SY::MINUTE
be another unit:- MOLAL
make
SY::MOLAL
its unit (but don't make it a standard unit…):- MOLAR
Standard unit of
SY::Molarity
is SY::MOLAR:- MOLE
And let
SY::MOLE
be its standard unit, related toSY::Amount
via Nᴀ:- MPH
Speed
¶ ↑- MagnitudeError
- Mass
Let
SY::Mass
be a standard quantity of basic dimension M:- Molality
- Molarity
SY::Molarity
is obtained by dividingSY::MoleAmount
by SY::LitreVolume:- MoleAmount
Let
SY::MoleAmount
be another dimensionless quantity:- Molecularity
- NAUTICAL_MILE
- NEWTON
This time, make
SY::NEWTON
its standard unit:- OUNCE
- PASCAL
make
SY::PASCAL
its standard unit:- PINT
Volume
¶ ↑- POUND
Mass
¶ ↑- PREFIX_TABLE
Table of standard prefixes and their corresponding unit multiples.
- Power
- Pressure
- QUART
FIXME:
PINT
=Unit.of
Volume
, amount: 568.26125.ml didn't work, it gave 1000 times more value something is wrong with the conversion mechanics- QuantityError
- SECOND
Let
SY::SECOND
be its standard unit:- SPEED_OF_LIGHT
Commonly used constant.
- SPS
SPS
stands for “superscripted product string”, It is a string of specific symbols with or without Unicode exponents, separated by periods, such as “syma.symb².symc⁻³.symd.syme⁴” etc. This closure takes 2 arguments (array of symbols, and array of exponents) and produces anSPS
out of them.- SPS_PARSER
A closure that parses superscripted product strings (SPSs). It takes 3 arguments: a string to be parsed, an array of acceptable symbols, and an array of acceptable prefixes. It returns 3 equal-sized arrays: prefixes, symbols and exponents.
- STANDARD_LABORATORY_TEMPERATURE
- STONE
- SUPERSCRIPT
Unicode superscript exponents.
- SUPERSCRIPT_DOWN
Reverse conversion of Unicode superscript exponents (from exponent strings to fixnums).
- SYNODIC_MONTH
Average lunar month.
- Speed
Define
SY::Speed
asSY::Length
/SY::Time
and make it a standard quantity of its dimension.- TON
Let
SY::TON
be another…- TRIPLE_POINT_OF_WATER
- Temperature
Let
SY::Temperature
be a standard quantity of basic dimension Θ:- Time
Let
SY::Time
be a standard quantity of basic dimension T:- UNIT
Let
SY::UNIT
be a standard unit ofSY::Amount
. Note that the upcase name of the constant “UNIT” implies, via YSupport's NameMagic mixin, that the name of the object becomes :unit and that it is possible to use syntax such as 42.unit to create magnitudes ofSY::Amount
.- VERSION
- VOLT
make
SY::VOLT
its standard unit:- Volume
Quantity
SY::Volume
is obtained by raising quantitySY::Length
to 3:- WATT
make
SY::WATT
its standard unit:- WATTHOUR
Watthour (Wh) is a common unit of energy.
- WEEK
SY::WEEK
means exactly seven days- YARD
- YEAR
Julian year.
Public Class Methods
Convenitence constructor of amounts (SY::Amount
if the standard dimensionless quantity of SY
).
# File lib/sy/fixed_assets_of_the_module.rb, line 270 def Amount number SY::Amount.relative.magnitude( number ) end
Convenience dimension accessor.
# File lib/sy/fixed_assets_of_the_module.rb, line 241 def Dimension id=proc{ return ::SY::Dimension }.call case id.to_s when '', 'nil', 'null', 'zero', '0', '⊘', '∅', 'ø' then SY::Dimension.zero else SY::Dimension.new id end end
Explicit magnitude constructor.
# File lib/sy/fixed_assets_of_the_module.rb, line 261 def Magnitude args=proc{ return ::SY::Magnitude }.call args.must_have :quantity, syn!: :of qnt = args.delete :quantity SY::Magnitude.of qnt, args end
Convenience quantity instance accessor.
# File lib/sy/fixed_assets_of_the_module.rb, line 249 def Quantity id=proc{ return ::SY::Quantity }.call SY::Quantity.instance id end
Convenience unit instance accessor.
# File lib/sy/fixed_assets_of_the_module.rb, line 255 def Unit id=proc{ return ::SY::Unit }.call SY::Unit.instance id end
Private Instance Methods
Convenitence constructor of amounts (SY::Amount
if the standard dimensionless quantity of SY
).
# File lib/sy/fixed_assets_of_the_module.rb, line 270 def Amount number SY::Amount.relative.magnitude( number ) end
Convenience dimension accessor.
# File lib/sy/fixed_assets_of_the_module.rb, line 241 def Dimension id=proc{ return ::SY::Dimension }.call case id.to_s when '', 'nil', 'null', 'zero', '0', '⊘', '∅', 'ø' then SY::Dimension.zero else SY::Dimension.new id end end
Explicit magnitude constructor.
# File lib/sy/fixed_assets_of_the_module.rb, line 261 def Magnitude args=proc{ return ::SY::Magnitude }.call args.must_have :quantity, syn!: :of qnt = args.delete :quantity SY::Magnitude.of qnt, args end
Convenience quantity instance accessor.
# File lib/sy/fixed_assets_of_the_module.rb, line 249 def Quantity id=proc{ return ::SY::Quantity }.call SY::Quantity.instance id end
Convenience unit instance accessor.
# File lib/sy/fixed_assets_of_the_module.rb, line 255 def Unit id=proc{ return ::SY::Unit }.call SY::Unit.instance id end