Project

unit

0.01
No commit activity in last 3 years
No release in over 3 years
Scientific unit support for ruby for calculations
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

>= 0.8.7
>= 0
 Project Readme

README

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.

Examples

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')

Mathematics

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')

Conversions

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.

Maintainers

Daniel Mendler and Chris Cashwell