Systeme - Dimensional declaration and conversion
Now with more drachm!
Systeme provides an extensive but intuitive library of convenience methods for handling and passing around numeric data that represents dimensioned units (such as length, mass, and volume). Besides trimming down your code and making it more readable, it simplifies the task of storing data such that all values are stored in their standard SI base unit (meter, kilogram, etc).
Declaration methods are the basis for utilizing systeme. Whenever a Numeric is declared with a systeme method, it will return an equivalent value in the SI standard base unit. These methods can be taken advantage of in several ways:
height_of_eiffel_tower = 325.meters => 325 # 325 already represents the SI standard unit meter, so this just helps you read your code more easily
10.kilometers => 10000 # as in, 10 kilometers equals 10000 meters # This allows you to quickly convert a number to it's equivalent base unit for storage 120.miles + 14.furlongs + 191.megameters => 191195937.632 # this value is in meters # Declarations also make otherwise annoying calculations a snap Systeme supports the five SI base units (meter, kilogram, ampere, second, kelvin) and all prefixed derivatives (pico-, nano-, kilo-, terra-, giga-, etc). Declarations can be made using any of a number of methods provided by these units.
10.meter == 10.meters == 10.m 10.kilogram == 10.kilograms == 10.kg A growing number of U.S, Imperial, deprecated, and other non international standard units are also supported.
10.pounds == 10.pound = 10.lb == 10.lbs == 10.lbm 10.inches == 10.inch == 10.in 10.foot == 10.feet == 10.foots == 10.ft
Conversions allow you to display or pass data in a unit other than the SI base. The set of methods for conversions is identical to declarations, but with each one prefixed by 'in_'. By declaring the numeric before converting it, any unit can be converted to any other unit. Non-declared numerics will be presumed to already be in their SI base unit
1000.meters.in_kilometer => 1 16.oz.in_lb => 1 5280.in_miles => 3.28083989501312 # 5280 was presumed to be in meters, if you were looking for a result of 1 you should do 5280.feet.in_miles
One useful application of systeme is to take human readable input to produce usable and storable data
The parse method provides very lax natural language parsing of dimensioned numbers in strings. It works much as one would expect, but two things to note: it will simply ignore any numbers associated with labels it doesn't understand, and it doesn't care if all the numbers in the string are of the same dimensionality. A string like “1 meter 1 centimeter” 1.01 and the string “1 meter 1 kilogram” will return 2.0. The same unit label can appear many times in a single string (“1m 1m 1m” => 3.0). If the parser finds a number with no label, it is technically being dimensioned as 'meter', which has no effect on the value.
As a gem
The official distributions of this gem are available on RubyForge under the project name systeme
this will be available soon
$ gem install systeme Development can be followed on GitHub at www.github.com/farski/systeme
$ gem install farski-systeme
Systeme is maintained by Chris Kalafarski
Systeme is Copyright © 2008 Christopher P Kalafarski. It is free software, and may be redistributed under the terms specified in the LICENSE file.