Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
96 lines (59 sloc) 3.25 KB


Classes for representing and manipulating physical quantities (numbers with units).

Written by Alex Reisner (


In scientific and engineering applications there is often a need to work with numbers that represent actual real-world things. Quantities (like 42) become “physical” when we attach units to them (like meters or pounds). They then represent properties of real objects like time, mass, length, temperature, etc. PhysicalQuantifier allows your numbers to retain their “physicality” whether they're stored in a database, loaded into memory, or manipulated mathematically and combined with other quantities of like and unlike units. It also facilitates easy conversion between units of different measurement systems.

PhysicalQuantifier is no longer actively maintained.


Install PhysicalQuantifier from the Git repository with the Rails plugin script:

script/plugin install git://

or outside of Rails without the plugin script:

git clone git://

Calculations and Conversions

Examples of basic usage:

# Define some physical quantities.
dist =, :m) # 53.125 meters
time = 4.25,  :s) #  4.250 seconds

# Calculate a rate.
speed = dist / time
  => "12.5 m/s"

# Convert to different units.
speed.convert_to(:km => 1, :h => -1)
  => "0.125 km/hr"

# Compare to another rate (with different but compatible units).
old_speed =, {:m => 1, :s => -1) # 2.1 m/s
speed > old_speed
  => true

# Display with HTML formatting.
accel =, :m => 1, :s => -2) # 48 m/s^2
  => "48 m/s<sup>2</sup>"

# Define custom unit (femtometer = meter x 10^-15). :fm, 'femtometer', :m, 1e-15
# You can now use femtometers in calculations, conversions, etc.

Model Integration

To add physical quantities to your existing Rails model (make raw numbers stored in your database “physical”):

extend PhysicalQuantifier physical_quantity 'mm', :depth, :width, :thickness physical_quantity 'mm^2', :coating_area, :surface_area physical_quantity 'dm^4', :second_moment_of_area_x, :second_moment_of_area_y physical_quantity 'kg/m', :weight_per_length

The above declarations will add methods like these to your model, which will return PhysicalQuantity objects instead of raw numbers:


If you'd prefer to have your default getter methods return PhysicalQuantity objects, place this declaration after you extend your model and before you declare your physical quantities:


This will cause the default getter methods (depth, width, thickness, etc) to return PhysicalQuantity objects, and define these methods for accessing the raw numbers stored in your database:


To-do List

  • Add MeasurementSystem class.

  • Add rounding option to to_s method.

  • Add support for a “displayable” unit symbol for use in to_s method.

Copyright © 2008 Alex Reisner (, released under the MIT license.

Something went wrong with that request. Please try again.