Unit support for numbers
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib prepare new version Jun 9, 2015
spec prepare new version Jun 9, 2015
.gitignore gitignore Jun 9, 2015
.travis.yml update travis Jun 9, 2015
Gemfile switch gemfile to https Mar 1, 2013
LICENSE prepare new version Jun 9, 2015
README.markdown update badge Feb 14, 2013
Rakefile prepare new version Jun 9, 2015
unit.gemspec gemspec Jun 9, 2015



Unit introduces computational units to Ruby. It offers built-in support for binary, mathematical, SI, imperial, scientific and temporal units, and a simple interface for adding your own, custom units.

Gem Version Dependency Status Build Status Code Climate

  • Define units for operands to avoid the inevitable mistakes that plague unit-less operations.
  • Perform complex mathematical operations while respecting the units of each operand.
  • Get meaningful errors when units aren't compatible.
  • Convert values between different systems of units with ease.


General Usage

require 'unit'
puts 1.meter.in_kilometer
puts 1.MeV.in_joule
puts 10.KiB / 1.second
puts 10.KiB_per_second
puts Unit('1 m/s^2')


Unit(1, 'km') + Unit(500, 'm') == Unit(1.5, 'km')
Unit(1, 'kg') - Unit(500, 'g') == Unit(0.5, 'kg')
Unit(100, 'miles/hour') * Unit(0.5, 'hours') == Unit('50 mi')
Unit(5.5, 'feet') / 2 == Unit(2.75, 'feet')
Unit(2, 'm') ** 2 == Unit(4, 'm^2')


Unit(1, 'mile').in('km') == Unit(1.609344,  'km')
(Unit(10, 'A') * Unit(0.1, 'volt')).in('watts') == Unit(1, 'watt')

See the test cases for many more examples.


Daniel Mendler and Chris Cashwell