Class Unit
In: lib/ruby_units/units.rb
lib/ruby_units/ruby-units.rb
Parent: Numeric

Ruby Units

Copyright 2006 by Kevin C. Olbrich, Ph.D.

See rubyforge.org/ruby-units/

www.sciwerks.org

kevin.olbrich+ruby-units@gmail.com

See README for detailed usage instructions and examples

Unit Definition Format

 '<name>'  => [%w{prefered_name synonyms}, conversion_to_base, :classification, %w{<base> <units> <in> <numerator>} , %w{<base> <units> <in> <denominator>} ],

Prefixes (e.g., a :prefix classification) get special handling Note: The accuracy of unit conversions depends on the precision of the conversion factor. If you have more accurate estimates for particular conversion factors, please send them to me and I will incorporate them into the next release. It is also incumbent on the end-user to ensure that the accuracy of any conversions is sufficient for their intended application.

While there are a large number of unit specified in the base package, there are also a large number of units that are not included. This package covers nearly all SI, Imperial, and units commonly used in the United States. If your favorite units are not listed here, send me an email

To add / override a unit definition, add a code block like this..

 class Unit < Numeric
  UNIT_DEFINITIONS = {
   <name>'  => [%w{prefered_name synonyms}, conversion_to_base, :classification, %w{<base> <units> <in> <numerator>} , %w{<base> <units> <in> <denominator>} ]
   }
 end
 Unit.setup

Methods

*   **   +   -   -@   /   <=>   ===   =~   >>   abs   after   ago   base_unit_cache   before   before_now   cached   ceil   clear_cache   coerce   compatible?   compatible_with?   convert_to   copy   floor   from   from_now   inspect   inverse   is_base?   is_temperature?   kind   kind_of?   new   power   root   round   same?   same_as?   setup   since   succ   temperature_scale   time   to   to_base   to_c   to_date   to_datetime   to_f   to_i   to_int   to_s   to_time   to_unit   to_yaml   to_yaml_properties   truncate   unit   unit_signature_vector   unitless?   units   until   update_base_scalar   zero?  

Included Modules

Comparable

Constants

UNIT_DEFINITIONS = { # prefixes '<googol>' => [%w{googol}, 1e100, :prefix], '<kibi>' => [%w{Ki Kibi kibi}, 2**10, :prefix], '<mebi>' => [%w{Mi Mebi mebi}, 2**20, :prefix], '<gibi>' => [%w{Gi Gibi gibi}, 2**30, :prefix], '<tebi>' => [%w{Ti Tebi tebi}, 2**40, :prefix], '<pebi>' => [%w{Pi Pebi pebi}, 2**50, :prefix], '<exi>' => [%w{Ei Exi exi}, 2**60, :prefix], '<zebi>' => [%w{Zi Zebi zebi}, 2**70, :prefix], '<yebi>' => [%w{Yi Yebi yebi}, 2**80, :prefix], '<yotta>' => [%w{Y Yotta yotta}, 1e24, :prefix], '<zetta>' => [%w{Z Zetta zetta}, 1e21, :prefix], '<exa>' => [%w{E Exa exa}, 1e18, :prefix], '<peta>' => [%w{P Peta peta}, 1e15, :prefix], '<tera>' => [%w{T Tera tera}, 1e12, :prefix], '<giga>' => [%w{G Giga giga}, 1e9, :prefix], '<mega>' => [%w{M Mega mega}, 1e6, :prefix], '<kilo>' => [%w{k kilo}, 1e3, :prefix], '<hecto>' => [%w{h Hecto hecto}, 1e2, :prefix], '<deca>' => [%w{da Deca deca deka}, 1e1, :prefix], '<deci>' => [%w{d Deci deci}, 1e-1, :prefix], '<centi>' => [%w{c Centi centi}, 1e-2, :prefix], '<milli>' => [%w{m Milli milli}, 1e-3, :prefix], '<micro>' => [%w{u Micro micro}, 1e-6, :prefix], '<nano>' => [%w{n Nano nano}, 1e-9, :prefix], '<pico>' => [%w{p Pico pico}, 1e-12, :prefix], '<femto>' => [%w{f Femto femto}, 1e-15, :prefix], '<atto>' => [%w{a Atto atto}, 1e-18, :prefix], '<zepto>' => [%w{z Zepto zepto}, 1e-21, :prefix], '<yocto>' => [%w{y Yocto yocto}, 1e-24, :prefix], '<1>' => [%w{1},1,:prefix], # length units '<meter>' => [%w{m meter meters metre metres}, 1.0, :length, %w{<meter>} ], '<inch>' => [%w{in inch inches "}, 0.0254, :length, %w{<meter>}], '<foot>' => [%w{ft foot feet '}, 0.3048, :length, %w{<meter>}], '<yard>' => [%w{yd yard yards}, 0.9144, :length, %w{<meter>}], '<mile>' => [%w{mi mile miles}, 1609.344, :length, %w{<meter>}], '<naut-mile>' => [%w{nmi}, 1852, :length, %w{<meter>}], '<league>'=> [%w{league leagues}, 4828, :length, %w{<meter>}], '<furlong>'=> [%w{furlong furlongs}, 201.2, :length, %w{<meter>}], '<rod>' => [%w{rd rod rods}, 5.029, :length, %w{<meter>}], '<mil>' => [%w{mil mils}, 0.0000254, :length, %w{<meter>}], '<angstrom>' =>[%w{ang angstrom angstroms}, 1e-10, :length, %w{<meter>}], '<fathom>' => [%w{fathom fathoms}, 1.829, :length, %w{<meter>}], '<pica>' => [%w{pica picas}, 0.004217, :length, %w{<meter>}], '<point>' => [%w{pt point points}, 0.0003514, :length, %w{<meter>}], '<redshift>' => [%w{z red-shift}, 1.302773e26, :length, %w{<meter>}], '<AU>' => [%w{AU astronomical-unit}, 149597900000, :length, %w{<meter>}], '<light-second>'=>[%w{ls light-second}, 299792500, :length, %w{<meter>}], '<light-minute>'=>[%w{lmin light-minute}, 17987550000, :length, %w{<meter>}], '<light-year>' => [%w{ly light-year}, 9460528000000000, :length, %w{<meter>}], '<parsec>' => [%w{pc parsec parsecs}, 30856780000000000, :length, %w{<meter>}], #mass '<kilogram>' => [%w{kg kilogram kilograms}, 1.0, :mass, %w{<kilogram>}], '<AMU>' => [%w{u AMU amu}, 6.0221415e26, :mass, %w{<kilogram>}], '<dalton>' => [%w{Da Dalton Daltons dalton daltons}, 6.0221415e26, :mass, %w{<kilogram>}], '<slug>' => [%w{slug slugs}, 14.5939029, :mass, %w{<kilogram>}], '<short-ton>' => [%w{tn ton}, 907.18474, :mass, %w{<kilogram>}], '<metric-ton>'=>[%w{tonne}, 1000, :mass, %w{<kilogram>}], '<carat>' => [%w{ct carat carats}, 0.0002, :mass, %w{<kilogram>}], '<pound-mass>' => [%w{lbs lb pound pounds #}, 0.45359237, :mass, %w{<kilogram>}], '<ounce>' => [%w{oz ounce ounces}, 0.0283495231, :mass, %w{<kilogram>}], '<gram>' => [%w{g gram grams gramme grammes},1e-3,:mass, %w{<kilogram>}], #area '<hectare>'=>[%w{hectare}, 10000, :area, %w{<meter> <meter>}], '<acre>'=>[%w(acre acres), 4046.85642, :area, %w{<meter> <meter>}], '<sqft>'=>[%w(sqft), 1, :area, %w{<feet> <feet>}], #volume '<liter>' => [%w{l L liter liters litre litres}, 0.001, :volume, %w{<meter> <meter> <meter>}], '<gallon>'=> [%w{gal gallon gallons}, 0.0037854118, :volume, %w{<meter> <meter> <meter>}], '<quart>'=> [%w{qt quart quarts}, 0.00094635295, :volume, %w{<meter> <meter> <meter>}], '<pint>'=> [%w{pt pint pints}, 0.000473176475, :volume, %w{<meter> <meter> <meter>}], '<cup>'=> [%w{cu cup cups}, 0.000236588238, :volume, %w{<meter> <meter> <meter>}], '<fluid-ounce>'=> [%w{floz fluid-ounce}, 2.95735297e-5, :volume, %w{<meter> <meter> <meter>}], '<tablespoon>'=> [%w{tbs tablespoon tablespoons}, 1.47867648e-5, :volume, %w{<meter> <meter> <meter>}], '<teaspoon>'=> [%w{tsp teaspoon teaspoons}, 4.92892161e-6, :volume, %w{<meter> <meter> <meter>}], #speed '<kph>' => [%w{kph}, 0.277777778, :speed, %w{<meter>}, %w{<second>}], '<mph>' => [%w{mph}, 0.44704, :speed, %w{<meter>}, %w{<second>}], '<knot>' => [%w{kt kn kts knot knots}, 0.514444444, :speed, %w{<meter>}, %w{<second>}], '<fps>' => [%w{fps}, 0.3048, :speed, %w{<meter>}, %w{<second>}], #acceleration '<gee>' => [%w{gee}, 9.80655, :acceleration, %w{<meter>}, %w{<second> <second>}], #temperature_difference '<kelvin>' => [%w{degK kelvin}, 1.0, :temperature, %w{<kelvin>}], '<celsius>' => [%w{degC celsius celsius centigrade}, 1.0, :temperature, %w{<kelvin>}], '<fahrenheit>' => [%w{degF fahrenheit}, 1/1.8, :temperature, %w{<kelvin>}], '<rankine>' => [%w{degR rankine}, 1/1.8, :temperature, %w{<kelvin>}], '<temp-K>' => [%w{tempK}, 1.0, :temperature, %w{<temp-K>}], '<temp-C>' => [%w{tempC}, 1.0, :temperature, %w{<temp-K>}], '<temp-F>' => [%w{tempF}, 1/1.8, :temperature, %w{<temp-K>}], '<temp-R>' => [%w{tempR}, 1/1.8, :temperature, %w{<temp-K>}], #time '<second>'=> [%w{s sec second seconds}, 1.0, :time, %w{<second>}], '<minute>'=> [%w{min minute minutes}, 60.0, :time, %w{<second>}], '<hour>'=> [%w{h hr hrs hour hours}, 3600.0, :time, %w{<second>}], '<day>'=> [%w{d day days}, 3600*24, :time, %w{<second>}], '<week>'=> [%w{wk week weeks}, 7*3600*24, :time, %w{<second>}], '<fortnight>'=> [%w{fortnight fortnights}, 1209600, :time, %W{<second>}], '<year>'=> [%w{y yr year years annum}, 31556926, :time, %w{<second>}], '<decade>'=>[%w{decade decades}, 315569260, :time, %w{<second>}], '<century>'=>[%w{century centuries}, 3155692600, :time, %w{<second>}], #pressure '<pascal>' => [%w{Pa pascal Pascal}, 1.0, :pressure, %w{<kilogram>},%w{<meter> <second> <second>}], '<bar>' => [%w{bar bars}, 100000, :pressure, %w{<kilogram>},%w{<meter> <second> <second>}], '<mmHg>' => [%w{mmHg}, 133.322368,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}], '<inHg>' => [%w{inHg}, 3386.3881472,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}], '<torr>' => [%w{torr}, 133.322368,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}], '<bar>' => [%w{bar}, 100000,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}], '<atm>' => [%w{atm ATM atmosphere atmospheres}, 101325,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}], '<psi>' => [%w{psi}, 6894.76,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}], '<cmh2o>' => [%w{cmH2O}, 98.0638,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}], '<inh2o>' => [%w{inH2O}, 249.082052,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}], #viscosity '<poise>' => [%w{P poise}, 0.1, :viscosity, %w{<kilogram>},%w{<meter> <second>} ], '<stokes>' => [%w{St stokes}, 1e-4, :viscosity, %w{<meter> <meter>}, %w{<second>}], #substance '<mole>' => [%w{mol mole}, 1.0, :substance, %w{<mole>}], #concentration '<molar>' => [%w{M molar}, 1000, :concentration, %w{<mole>}, %w{<meter> <meter> <meter>}], '<wtpercent>' => [%w{wt% wtpercent}, 10, :concentration, %w{<kilogram>}, %w{<meter> <meter> <meter>}], #activity '<katal>' => [%w{kat katal Katal}, 1.0, :activity, %w{<mole>}, %w{<second>}], '<unit>' => [%w{U enzUnit}, 16.667e-16, :activity, %w{<mole>}, %w{<second>}], #capacitance '<farad>' => [%w{F farad Farad}, 1.0, :capacitance, %w{<farad>}], #charge '<coulomb>' => [%w{C coulomb Coulomb}, 1.0, :charge, %w{<ampere> <second>}], #current '<ampere>' => [%w{A Ampere ampere amp amps}, 1.0, :current, %w{<ampere>}], #conductance '<siemens>' => [%w{S Siemens siemens}, 1.0, :resistance, %w{<second> <second> <second> <ampere> <ampere>}, %w{<kilogram> <meter> <meter>}], #inductance '<henry>' => [%w{H Henry henry}, 1.0, :inductance, %w{<meter> <meter> <kilogram>}, %w{<second> <second> <ampere> <ampere>}], #potential '<volt>' => [%w{V Volt volt volts}, 1.0, :potential, %w{<meter> <meter> <kilogram>}, %w{<second> <second> <second> <ampere>}], #resistance '<ohm>' => [%w{Ohm ohm}, 1.0, :resistance, %w{<meter> <meter> <kilogram>},%w{<second> <second> <second> <ampere> <ampere>}], #magnetism '<weber>' => [%w{Wb weber webers}, 1.0, :magnetism, %w{<meter> <meter> <kilogram>}, %w{<second> <second> <ampere>}], '<tesla>' => [%w{T tesla teslas}, 1.0, :magnetism, %w{<kilogram>}, %w{<second> <second> <ampere>}], '<gauss>' => [%w{G gauss}, 1e-4, :magnetism, %w{<kilogram>}, %w{<second> <second> <ampere>}], '<maxwell>' => [%w{Mx maxwell maxwells}, 1e-8, :magnetism, %w{<meter> <meter> <kilogram>}, %w{<second> <second> <ampere>}], '<oersted>' => [%w{Oe oersted oersteds}, 250.0/Math::PI, :magnetism, %w{<ampere>}, %w{<meter>}], #energy '<joule>' => [%w{J joule Joule joules}, 1.0, :energy, %w{<meter> <meter> <kilogram>}, %w{<second> <second>}], '<erg>' => [%w{erg ergs}, 1e-7, :energy, %w{<meter> <meter> <kilogram>}, %w{<second> <second>}], '<btu>' => [%w{BTU btu BTUs}, 1055.056, :energy, %w{<meter> <meter> <kilogram>}, %w{<second> <second>}], '<calorie>' => [%w{cal calorie calories}, 4.18400, :energy,%w{<meter> <meter> <kilogram>}, %w{<second> <second>}], '<Calorie>' => [%w{Cal Calorie Calories}, 4184.00, :energy,%w{<meter> <meter> <kilogram>}, %w{<second> <second>}], '<therm-US>' => [%w{th therm therms Therm}, 105480400, :energy,%w{<meter> <meter> <kilogram>}, %w{<second> <second>}], #force '<newton>' => [%w{N Newton newton}, 1.0, :force, %w{<kilogram> <meter>}, %w{<second> <second>}], '<dyne>' => [%w{dyn dyne}, 1e-5, :force, %w{<kilogram> <meter>}, %w{<second> <second>}], '<pound-force>' => [%w{lbf pound-force}, 4.448222, :force, %w{<kilogram> <meter>}, %w{<second> <second>}], #frequency '<hertz>' => [%w{Hz hertz Hertz}, 1.0, :frequency, %w{<1>}, %{<second>}], #angle '<radian>' =>[%w{rad radian radian}, 1.0, :angle, %w{<radian>}], '<degree>' =>[%w{deg degree degrees}, Math::PI / 180.0, :angle, %w{<radian>}], '<grad>' =>[%w{grad gradian grads}, Math::PI / 200.0, :angle, %w{<radian>}], '<steradian>' => [%w{sr steradian steradians}, 1.0, :solid_angle, %w{<steradian>}], #rotation '<rotation>' => [%w{rotation}, 2.0*Math::PI, :angle, %w{<radian>}], '<rpm>' =>[%w{rpm}, 2.0*Math::PI / 60.0, :angular_velocity, %w{<radian>}, %w{<second>}], #memory '<byte>' =>[%w{B byte}, 1.0, :memory, %w{<byte>}], '<bit>' =>[%w{b bit}, 0.125, :memory, %w{<byte>}], #currency '<dollar>'=>[%w{USD dollar}, 1.0, :currency, %w{<dollar>}], '<cents>' =>[%w{cents}, 0.01, :currency, %w{<dollar>}], #luminosity '<candela>' => [%w{cd candela}, 1.0, :luminosity, %w{<candela>}], '<lumen>' => [%w{lm lumen}, 1.0, :luminous_power, %w{<candela> <steradian>}], '<lux>' =>[%w{lux}, 1.0, :illuminance, %w{<candela> <steradian>}, %w{<meter> <meter>}], #power '<watt>' => [%w{W watt watts}, 1.0, :power, %w{<kilogram> <meter> <meter>}, %w{<second> <second> <second>}], '<horsepower>' => [%w{hp horsepower}, 745.699872, :power, %w{<kilogram> <meter> <meter>}, %w{<second> <second> <second>}], #radiation '<gray>' => [%w{Gy gray grays}, 1.0, :radiation, %w{<meter> <meter>}, %w{<second> <second>}], '<roentgen>' => [%w{R roentgen}, 0.009330, :radiation, %w{<meter> <meter>}, %w{<second> <second>}], '<sievert>' => [%w{Sv sievert sieverts}, 1.0, :radiation, %w{<meter> <meter>}, %w{<second> <second>}], '<becquerel>' => [%w{Bq bequerel bequerels}, 1.0, :radiation, %w{<1>},%w{<second>}], '<curie>' => [%w{Ci curie curies}, 3.7e10, :radiation, %w{<1>},%w{<second>}], # rate '<cpm>' => [%w{cpm}, 1.0/60.0, :rate, %w{<count>},%w{<second>}], '<dpm>' => [%w{dpm}, 1.0/60.0, :rate, %w{<count>},%w{<second>}], '<bpm>' => [%w{bpm}, 1.0/60.0, :rate, %w{<count>},%w{<second>}], #resolution / typography '<dot>' => [%w{dot dots}, 1, :resolution, %w{<each>}], '<pixel>' => [%w{pixel px}, 1, :resolution, %w{<each>}], '<ppi>' => [%w{ppi}, 1, :resolution, %w{<pixel>}, %w{<inch>}], '<dpi>' => [%w{dpi}, 1, :typography, %w{<dot>}, %w{<inch>}], '<pica>' => [%w{pica}, 0.00423333333 , :typography, %w{<meter>}], '<point>' => [%w{point pt}, 0.000352777778, :typography, %w{<meter>}], #other '<cell>' => [%w{cells cell}, 1, :counting, %w{<each>}], '<each>' => [%w{each}, 1.0, :counting, %w{<each>}], '<count>' => [%w{count}, 1.0, :counting, %w{<each>}], '<base-pair>' => [%w{bp}, 1.0, :counting, %w{<each>}], '<nucleotide>' => [%w{nt}, 1.0, :counting, %w{<each>}], '<molecule>' => [%w{molecule molecules}, 1.0, :counting, %w{<1>}], '<dozen>' => [%w{doz dz dozen},12.0,:prefix_only, %w{<each>}], '<percent>'=> [%w{% percent}, 0.01, :prefix_only, %w{<1>}], '<ppm>' => [%w{ppm},1e-6,:prefix_only, %w{<1>}], '<ppt>' => [%w{ppt},1e-9,:prefix_only, %w{<1>}], '<gross>' => [%w{gr gross},144.0, :prefix_only, %w{<dozen> <dozen>}], '<decibel>' => [%w{dB decibel decibels}, 1.0, :logarithmic, %w{<decibel>}]
VERSION = '1.1.0'   pre-generate hashes from unit definitions for performance.
UNITY = '<1>'
UNITY_ARRAY = [UNITY]
FEET_INCH_REGEX = /(\d+)\s*(?:'|ft|feet)\s*(\d+)\s*(?:"|in|inches)/
TIME_REGEX = /(\d+)*:(\d+)*:*(\d+)*[:,]*(\d+)*/
LBS_OZ_REGEX = /(\d+)\s*(?:#|lbs|pounds)+[\s,]*(\d+)\s*(?:oz|ounces)/
SCI_NUMBER = %r{([+-]?\d*[.]?\d+(?:[Ee][+-]?)?\d*)}
RATIONAL_NUMBER = /(\d+)\/(\d+)/
COMPLEX_NUMBER = /#{SCI_NUMBER}?#{SCI_NUMBER}i\b/
NUMBER_REGEX = /#{SCI_NUMBER}*\s*(.+)?/
UNIT_STRING_REGEX = /#{SCI_NUMBER}*\s*([^\/]*)\/*(.+)*/
TOP_REGEX = /([^ \*]+)(?:\^|\*\*)([\d-]+)/
BOTTOM_REGEX = /([^* ]+)(?:\^|\*\*)(\d+)/
UNCERTAIN_REGEX = /#{SCI_NUMBER}\s*\+\/-\s*#{SCI_NUMBER}\s(.+)/
COMPLEX_REGEX = /#{COMPLEX_NUMBER}\s?(.+)?/
RATIONAL_REGEX = /#{RATIONAL_NUMBER}\s?(.+)?/
KELVIN = ['<kelvin>']
FAHRENHEIT = ['<fahrenheit>']
RANKINE = ['<rankine>']
CELSIUS = ['<celsius>']
SIGNATURE_VECTOR = [:length, :time, :temperature, :mass, :current, :substance, :luminosity, :currency, :memory, :angle, :capacitance]

Attributes

base_denominator  [RW] 
base_numerator  [RW] 
base_scalar  [RW] 
denominator  [RW] 
numerator  [RW] 
output  [RW] 
scalar  [RW] 
signature  [RW] 
unit_name  [RW] 

Public Class methods

Create a new Unit object. Can be initialized using a string, or a hash Valid formats include:

 "5.6 kg*m/s^2"
 "5.6 kg*m*s^-2"
 "5.6 kilogram*meter*second^-2"
 "2.2 kPa"
 "37 degC"
 "1"  -- creates a unitless constant with value 1
 "GPa"  -- creates a unit with scalar 1 with units 'GPa'
 6'4"  -- recognized as 6 feet + 4 inches
 8 lbs 8 oz -- recognized as 8 lbs + 8 ounces

Public Instance methods

Multiply two units.

Exponentiate. Only takes integer powers. Note that anything raised to the power of 0 results in a Unit object with a scalar of 1, and no units. Throws an exception if exponent is not an integer. Ideally this routine should accept a float for the exponent It should then convert the float to a rational and raise the unit by the numerator and root it by the denominator but, sadly, floats can’t be converted to rationals.

For now, if a rational is passed in, it will be used, otherwise we are stuck with integers and certain floats < 1

Add two units together. Result is same units as receiver and scalar and base_scalar are updated appropriately throws an exception if the units are not compatible. It is possible to add Time objects to units of time

Subtract two units. Result is same units as receiver and scalar and base_scalar are updated appropriately throws an exception if the units are not compatible.

negates the scalar of the Unit

Divide two units. Throws an exception if divisor is 0

Compare two Unit objects. Throws an exception if they are not of compatible types. Comparisons are done based on the value of the unit in base SI units.

Compare two units. Returns true if quantities and units match

Unit("100 cm") === Unit("100 cm") # => true Unit("100 cm") === Unit("1 m") # => false

check to see if units are compatible, but not the scalar part this check is done by comparing signatures for performance reasons if passed a string, it will create a unit object with the string and then do the comparison this permits a syntax like:

 unit =~ "mm"

if you want to do a regexp on the unit string do this …

 unit.units =~ /regexp/
>>(other)

Alias for to

returns abs of scalar, without the units

after(time_point = ::Time.now)

Alias for from

‘5 min’.unit.ago

‘5 min’.before(time)

before_now(time_point = ::Time.now)

Alias for before

automatically coerce objects to units when possible if an object defines a ‘to_unit’ method, it will be coerced using that method

compatible?(other)

Alias for #=~

compatible_with?(other)

Alias for #=~

convert_to(other)

Alias for to

‘5 min’.from(time)

from_now(time_point = ::Time.now)

Alias for from

Normally pretty prints the unit, but if you really want to see the guts of it, pass ’:dump’

returns inverse of Unit (1/unit)

Returns ‘true’ if the Unit is represented in base units

needed to make complex units play nice — otherwise not detected as a complex_generic

returns the unit raised to the n-th power. Integers only

Calculates the n-th root of a unit, where n = (1..9) if n < 0, returns 1/unit^(1/n)

same?(other)

Alias for #===

same_as?(other)

Alias for #===

‘min’.since(time)

returns next unit in a range. ‘1 mm’.unit.succ #=> ‘2 mm’.unit only works when the scalar is an integer

time()

Alias for to_time

convert to a specified unit string or to the same units as another Unit

 unit >> "kg"  will covert to kilograms
 unit1 >> unit2 converts to same units as unit2 object

To convert a Unit object to match another Unit object, use:

 unit1 >>= unit2

Throws an exception if the requested target units are incompatible with current Unit.

Special handling for temperature conversions is supported. If the Unit object is converted from one temperature unit to another, the proper temperature offsets will be used. Supports Kelvin, Celcius, fahrenheit, and Rankine scales.

Note that if temperature is part of a compound unit, the temperature will be treated as a differential and the units will be scaled appropriately.

convert to base SI units results of the conversion are cached so subsequent calls to this will be fast

converts the unit back to a complex if it is unitless. Otherwise raises an exception

convert a duration to a DateTime. This will work so long as the duration is the duration from the zero date defined by DateTime

converts the unit back to a float if it is unitless. Otherwise raises an exception

if unitless, returns an int, otherwise raises an error

to_int()

Alias for to_i

Generate human readable output. If the name of a unit is passed, the unit will first be converted to the target unit before output. some named conversions are available

 :ft - outputs in feet and inches (e.g., 6'4")
 :lbs - outputs in pounds and ounces (e.g, 8 lbs, 8 oz)

You can also pass a standard format string (i.e., ’%0.2f’) or a strftime format string.

output is cached so subsequent calls for the same format will be fast

Tries to make a Time object from current unit. Assumes the current unit hold the duration in seconds from the epoch.

basically a copy of the basic to_yaml. Needed because otherwise it ends up coercing the object to a string before YAML‘izing it.

unit()

Alias for to_unit

returns true if no associated units false, even if the units are "unitless" like ‘radians, each, etc’

returns the ‘unit’ part of the Unit object without the scalar

‘min’.until(time)

true if scalar is zero

Protected Instance methods

calculates the unit signature vector used by unit_signature

Protected and Private Functions that should only be called from this class

[Validate]