alexreisner / physical_quantifier
- Source
- Commits
- Network (0)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Branch:
master
| name | age | message | |
|---|---|---|---|
| |
MIT-LICENSE | ||
| |
README.rdoc | ||
| |
Rakefile | ||
| |
init.rb | ||
| |
lib/ | ||
| |
tasks/ | ||
| |
test/ |
PhysicalQuantifier
Classes for representing and manipulating physical quantities (numbers with units).
Written by Alex Reisner (alex@alexreisner.com).
Summary
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.
Installation
Install PhysicalQuantifier from the Git repository with the Rails plugin script:
script/plugin install git://github.com/alexreisner/physical_quantifier.git
or outside of Rails without the plugin script:
git clone git://github.com/alexreisner/physical_quantifier.git
Calculations and Conversions
Examples of basic usage:
# Define some physical quantities.
dist = PhysicalQuantity.new(53.125, :m) # 53.125 meters
time = PhysicalQuantity.new( 4.25, :s) # 4.250 seconds
# Calculate a rate.
speed = dist / time
speed.to_s
=> "12.5 m/s"
# Convert to different units.
speed.convert_to(:km => 1, :h => -1)
speed.to_s
=> "0.125 km/hr"
# Compare to another rate (with different but compatible units).
old_speed = PhysicalQuantity.new(2.1, {:m => 1, :s => -1) # 2.1 m/s
speed > old_speed
=> true
# Display with HTML formatting.
accel = PhysicalQuantity.new(48, :m => 1, :s => -2) # 48 m/s^2
accel.to_s(:html)
=> "48 m/s<sup>2</sup>"
# Define custom unit (femtometer = meter x 10^-15).
Unit.new :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:
depth_qty width_qty thickness_qty ...
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:
getters_return_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:
raw_depth raw_width raw_thickness ...
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 (alex@alexreisner.com), released under the MIT license.

