An extension to Ruby's Numeric classes that adds support for units of measure.
Copyright 2012-2014 Brandon Fosdick bfoz@bfoz.net and released under the BSD license.
require 'units'
three_meters = 3.meters
two_meters = 2.meters
one_inch = 1.inch
Note that you can't use 1.in
because 'in' is a reserved word in Ruby.
You can also make fancier units by passing arguments...
square_meters = 3.meters(2)
speed = 3.meters.second(-1)
3.meters.inches # => 118.1103 inch
10.inches.mm # => 254.0 millimeter
All of the SI units listed below allow any of the standard SI prefixes to be prepended to the unit name. Actually, the US Customary units support the SI prefixes too, but that's not a typical usage.
Note that the names listed here are the symbol names used by the Units gem, and are derived from the proper names of the units. They don't exactly match the proper unit names to maintain consistency and to respect Ruby's naming conventions.
- meter
- gram
- second
- ampere
- kelvin
- candela
- mole
- hertz
- radian
- steradian
- newton
- pascal
- joule
- watt
- coulomb
- volt
- farad
- ohm
- siemens
- weber
- tesla
- henry
- celsius
- lumen
- lux
- gray
- sievert
- katal
- becquerel
Some combinations of SI units and prefixes are both verbose and commonly used. Enough so that it makes sense to support their abbreviations. The supported abbreviations are listed below.
- mm -> millimeter
- cm -> centimeter
- km -> kilometer
Head over to the Wikipedia article on US Customary Units to learn about all of these bizare units and their namesakes.
- acre
- section
- township
- point
- pica
- inch
- foot
- yard
- mile
- grain
- dram
- ounce
- pound
- hundredweight
- long_hundredweight
- short_ton
- long_ton
- pennyweight
- troy_ounce
- troy_pound
- fathom
- cable
- nautical_mile
- link
- survey_foot
- rod
- chain
- furlong
- statute_mile
- league
- fahreheight
- rankine
- acre_foot
- minim
- fluid_dram
- teaspoon
- tablespoon
- fluid_ounce
- jigger
- gill
- cup
- pint
- quart
- barell
- hogshead