Module: UOM

Defined in:
lib/uom/measurement.rb,
lib/uom/unit.rb,
lib/uom/error.rb,
lib/uom/units.rb,
lib/uom/factor.rb,
lib/uom/factors.rb,
lib/uom/version.rb,
lib/uom/dimension.rb,
lib/uom/dimensions.rb,
lib/uom/unit_factory.rb,
lib/uom/composite_unit.rb,
lib/uom/composite_unit_key_canonicalizer.rb

Overview

UOM implements Units of Measurement based on the International System of Units. See [github.com/caruby/uom] for details.

Defined Under Namespace

Classes: CompositeDimension, CompositeUnit, CompositeUnitKeyCanonicalizer, Dimension, Factor, Measurement, MeasurementError, Unit, UnitFactory

Constant Summary collapse

METER =

the standard units for each dimension

Unit.new(:meter, :m, LENGTH, *METRIC_FACTORS)
GRAM =
Unit.new(:gram, :gm, :g, MASS, *METRIC_FACTORS)
SECOND =
Unit.new(:second, :sec, :s, TIME, MILLI, MICRO, NANO, PICO, FEMTO)
KELVIN =
Unit.new(:kelvin, :K, TEMPERATURE)
CANDELA =
Unit.new(:candela, :cd, INTENSITY)
AMPERE =
Unit.new(:ampere, :A, CURRENT, *ELECTRONIC_FACTORS)
MOLE =
Unit.new(:mole, :mol, MASS)
BYTE =

Scaled byte units, e.g KByte, are defined below

Unit.new(:byte, :Byte, :B, :b, INFORMATION)
MILLIMETER =

SCALED UNITS ## Just a few common metric units; units are typically referred to by label rather than a constant, e.g. Unit.for(:millimeter)

Unit.for(:millimeter)
CENTIMETER =
Unit.for(:centimeter)
DECIMETER =
Unit.for(:decimeter)
MICROGRAM =
Unit.for(:microgram)
MILLIGRAM =
Unit.for(:milligram)
KILOGRAM =
Unit.for(:kilogram)
KILOBYTE =

The BYTE scale is a base two axis multiplier rather than a metric scalar multipler. TODO - express these with base-2 factors?

Unit.new(:kilobyte, :KByte, :KB, BYTE, 1024)
MEGABYTE =
Unit.new(:megabyte, :MByte, :MB, KILOBYTE, 1024)
GIGABYTE =
Unit.new(:gigabyte, :GByte, :GB, MEGABYTE, 1024)
TERABYTE =
Unit.new(:terabyte, :TByte, :TB, GIGABYTE, 1024)
PETABYTE =
Unit.new(:petabyte, :PByte, :PB, TERABYTE, 1024)
ANGSTROM =

astronomical distances

Unit.new(:angstrom, :a, METER, 10000000000)
AU =
Unit.new(:astronomical_unit, :AU, METER, 149597870691)
LIGHT_YEAR =
Unit.new(:light_year, :ly, METER, 9460730472580800)
CELSIUS =

temperature with inverse converters

Unit.new(:celsius, :C, KELVIN) { |celsius| celsius - 273.15 }
FARENHEIT =
Unit.new(:farenheit, :F, CELSIUS) { |farenheit| (farenheit - 32) * (5 / 9) }
MINUTE =

time

Unit.new(:minute, :min, SECOND, 60)
HOUR =
Unit.new(:hour, :hr, MINUTE, 60)
DAY =
Unit.new(:day, HOUR, 24)
BIT =

information

Unit.new(:bit, BYTE, 1.0 / 8)
INCH =

US Customary length units

Unit.new(:inch, :in, METER, 0.0254)
FOOT =
Unit.new(:foot, :ft, INCH, 12)
YARD =
Unit.new(:yard, :yd, FOOT, 3)
MILE =
Unit.new(:mile, :mi, FOOT, 5280)
OUNCE =

US Customary weight units

Unit.new(:ounce, :oz, GRAM, 28.34952)
POUND =
Unit.new(:pound, :lb, OUNCE, 16)
TON =
Unit.new(:ton, POUND, 2000)
GRAIN =
Unit.new(:grain, :gr, POUND, 1.0 / 7000)
DRAM =
Unit.new(:dram, :dr, OUNCE, 1.0 / 16)
LITER =

Metric composite units

Unit.new(:liter, :l, :L, DECIMETER * DECIMETER * DECIMETER, *METRIC_FACTORS)
MILLILITER =
Unit.for(:milliliter)
JOULE =
Unit.new(:joule, :J, (KILOGRAM * (METER * METER)) / (SECOND * SECOND), *ELECTRONIC_FACTORS)
ERG =
Unit.new(:erg, (GRAM * (CENTIMETER * CENTIMETER)) / (SECOND * SECOND), *ELECTRONIC_FACTORS)
DYNE =
Unit.new(:dyne, :dyn, (GRAM * CENTIMETER) / (SECOND * SECOND), *ELECTRONIC_FACTORS)
NEWTON =
Unit.new(:newton, :N, (KILOGRAM * METER) / (SECOND * SECOND), *SIMPLE_FACTORS)
BAR =
Unit.new(:bar, NEWTON, 0.00001, *SIMPLE_FACTORS)
PASCAL =
Unit.new(:pascal, :Pa, NEWTON / (METER * METER), *SIMPLE_FACTORS)
ATMOSPHERE =
Unit.new(:atmosphere, :atm, PASCAL, 0.101325, *SIMPLE_FACTORS)
TORR =
Unit.new(:torr, ATMOSPHERE, 1.0 / 760, *SIMPLE_FACTORS)
BARYE =
Unit.new(:barye, :Ba, GRAM / (CENTIMETER * (SECOND * SECOND)), *ELECTRONIC_FACTORS)
POISE =
Unit.new(:poise, :P, GRAM / (CENTIMETER * SECOND), KILO, MILLI, MICRO, NANO, PICO)
COULOMB =

C abbreviation is taken by CENTIGRADE

Unit.new(:coulomb, AMPERE / SECOND, *ELECTRONIC_FACTORS)
VOLT =
Unit.new(:volt, :V, JOULE / COULOMB, *ELECTRONIC_FACTORS)
FARAD =

F abbreviation is taken by FARENHEIT

Unit.new(:farad, COULOMB / VOLT, *ELECTRONIC_FACTORS)
WEBER =
Unit.new(:weber, :Wb, VOLT * SECOND, *ELECTRONIC_FACTORS)
HENRY =
Unit.new(:henry, :H, WEBER / AMPERE, METRIC_FACTORS)
MAXWELL =
Unit.new(:maxwell, :Mx, WEBER, 1.0 / 10 ** 8, METRIC_FACTORS)
GAUSS =
Unit.new(:gauss, :G, MAXWELL / (CENTIMETER * CENTIMETER), METRIC_FACTORS)
TESLA =
Unit.new(:tesla, :T, WEBER / (METER * METER), DECI, CENTI, MILLI, MICRO, NANO, PICO)
C =

The speed of light in a vacuum in cm/sec.

29979245800
OHM =
Unit.new(:ohm, SECOND / CENTIMETER, 10 ** 9 / C)
FLUID_OUNCE =

US Customary liquid volume units

Unit.new(:fluid_ounce, :fl_oz, MILLILITER, 29.57353)
TBSP =
Unit.new(:tablespoon, :tbsp, FLUID_OUNCE, 2)
TSP =
Unit.new(:teaspoon, :tsp, TBSP, 1.0 / 3)
CUP =
Unit.new(:cup, :cp, FLUID_OUNCE, 8)
PINT =
Unit.new(:pint, :pt, CUP, 2)
QUART =
Unit.new(:quart, :qt, PINT, 2)
GALLON =
Unit.new(:gallon, :gal, QUART, 4)
DRY_PINT =

US Customary dry volume units

Unit.new(:dry_pint, :dry_pt, LITER) { |liter| liter / 0.5506105 }
DRY_QUART =
Unit.new(:dry_quart, :dry_qt, DRY_PINT, 2)
DRY_GALLON =
Unit.new(:dry_gallon, :dry_gal, DRY_QUART, 4)
PECK =
Unit.new(:peck, :pk, DRY_GALLON, 2)
BUSHEL =
Unit.new(:bushel, :bu, PECK, 4)
PSI =

Pressure alias

Unit.for(:pounds_per_square_inch).add_abbreviation(:psi)
UNIT =

the metric scaling factors

Factor.new(nil, nil, nil)
DECI =
Factor.new(:deci, :d, UNIT, 0.1)
CENTI =
Factor.new(:centi, :c, UNIT, 0.01)
MILLI =
Factor.new(:milli, :m, UNIT, 0.001)
MICRO =
Factor.new(:micro, :u, MILLI, 0.001)
NANO =
Factor.new(:nano, :n, MICRO, 0.001)
PICO =
Factor.new(:pico, :p, NANO, 0.001)
FEMTO =
Factor.new(:femto, :f, PICO, 0.001)
ATTO =
Factor.new(:atto, :a, FEMTO, 0.001)
ZEPTO =
Factor.new(:zepto, :z, ATTO, 0.001)
YOCTO =
Factor.new(:yocto, :y, ZEPTO, 0.001)
DECA =
Factor.new(:deca, :da, UNIT, 10)
HECTO =
Factor.new(:hecto, :h, UNIT, 100)
KILO =
Factor.new(:kilo, :k, UNIT, 1000)
MEGA =
Factor.new(:mega, :M, KILO, 1000)
GIGA =
Factor.new(:giga, :G, MEGA, 1000)
TERA =
Factor.new(:tera, :T, GIGA, 1000)
PETA =
Factor.new(:peta, :P, TERA, 1000)
EXA =
Factor.new(:exa, :E, PETA, 1000)
ZETTA =
Factor.new(:zetta, :Z, EXA, 1000)
YOTTA =
Factor.new(:yotta, :Y, ZETTA, 1000)
METRIC_FACTORS =

All metric factors.

[YOTTA, ZETTA, EXA, TERA, GIGA, MEGA, KILO, HECTO, DECA, DECI, CENTI, MILLI, MICRO, NANO, PICO, FEMTO, ATTO, ZEPTO, YOCTO]
ELECTRONIC_FACTORS =

Factors commonly used in electronics.

[MILLI, MICRO, NANO, PICO, TERA, GIGA, MEGA, KILO]
SIMPLE_FACTORS =

Factors commonly used for measures in a moderate range about 1.

[CENTI, MILLI, MEGA, KILO]
VERSION =
"1.2.2"
LENGTH =
Dimension.new(:length)
MASS =
Dimension.new(:mass)
TEMPERATURE =
Dimension.new(:temperature)
TIME =
Dimension.new(:time)
ENERGY =
Dimension.new(:energy)
INTENSITY =
Dimension.new(:intensity)
CURRENT =
Dimension.new(:current)
INFORMATION =
Dimension.new(:information)