0.0
No commit activity in last 3 years
No release in over 3 years
SimpleMeasures is an easy and extensible measurement conversion library
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

Simple Measures¶ ↑

all the units you need to describe and convert recipes.

volume¶ ↑

gallons, quarts, pints, cups, floz
liters

weight¶ ↑

pounds, ounces
grams

Motivation¶ ↑

Why did I choose to re-invent a small wheel when there are already a few measurement libs out there? I became dissatisfied with the libraries out there when I tried using them for a simple home brewing project. Some of the libs specialized in the variety of measurements available, others specialized in sugary syntax. I needed a library that would meet a few simple requirements.

  • well behaved ruby object - it defines methods like #<=>, #hash, #eql?, #coerce

  • using it’s own class - not just converting to integers

  • ability to choose what features are loaded - doesn’t load metric if I am only using imperial

Features¶ ↑

  • protects you from performing math on incompatible types

  • equality that knows 2.kilograms == 2000.grams

  • works well with Hash and Set classes

  • adds syntactic sugar to make your code easier to read

  • easy to use with Rails composed_of method

Examples¶ ↑

require 'simple-measures'

SimpleMeasures.use_imperial_units!
SimpleMeasures.use_si_units!
SimpleMeasures.use_numeric_sugar!

( 3.pounds + 2.oz ).to_grams
=> #<SimpleMeasures::Unit:0x00000101108e20 @value=1417.4761562, @unit=:grams>

1.liter.to_quarts
=> #<SimpleMeasures::Unit:0x00000101115568 @value=1.05668820943259, @unit=:quarts>

Extending¶ ↑

Do we not have what you need? Starting a new project that needs to measure blargs? SimpleMeasures is easy to extend.

# add_unit takes three things, the "unit name", the "unit value", and the type of measurement
# in this case, we are telling it that "blarg" is the base unit for alien weight
SimpleMeasures::Registry.add_unit( :blarg, 1, :alien_weight )

# now we tell SimpleMeasures that it's okay to use the name "blargs" to mean "blarg"
SimpleMeasures::Registry.add_alias( :blarg, :blargs )

# since we sometimes get heavy objects, we need kiloblargs
SimpleMeasures::Registry.add_unit( :kiloblarg, 10 ** 3, :alien_weight )

# it's also okay to call them "kiloblargs"
SimpleMeasures::Registry.add_alias( :kiloblarg, :kiloblargs )

# we also sometimes shorten them to "kbs"
SimpleMeasures::Registry.add_alias( :kiloblarg, :kbs )

# lets turn on some syntactic sugar!
SimpleMeasures.use_numeric_sugar!

3.kbs.to_blargs.to_s # => "3000.0 blargs"

Copyright © 2010 Colin Harris. See LICENSE for details.