bfoz / units-ruby

Add support for units of measure to Ruby's Numeric class
BSD 2-Clause "Simplified" License
5 stars 3 forks source link

Units for Ruby

Build Status

An extension to Ruby's Numeric classes that adds support for units of measure.

Examples

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

Converting units

    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

Supported Units

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

SI Abbreviations

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.

US Customary Units

Head over to the Wikipedia article on US Customary Units to learn about all of these bizare units and their namesakes.

Area

"International"

Mass

Nautical

Survey

Temperature

Volume

License

Copyright 2012-2015 Brandon Fosdick bfoz@bfoz.net and released under the BSD license.