Units for Ruby
An extension to Ruby's Numeric classes that adds support for units of measure.
Creating numbers with units
require 'units' three_meters = 3.meters two_meters = 2.m 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) gravity = 9.81.meters.per_second(2) future = 88.miles.per_hour # No need for roads hertz = 440.per_second # A lovely A4 note
3.meters.inches # => 118.1103 inch 10.inches.mm # => 254.0 millimeter
Checking for units
three_meters = 3.meters three_meters.meters? # => true three_meters.inches? # => false
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.
SI Primary Units
SI Derived Units
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
US Customary Units
Head over to the Wikipedia article on US Customary Units to learn about all of these bizare units and their namesakes.
Copyright 2012-2015 Brandon Fosdick firstname.lastname@example.org and released under the BSD license.