measurement: The Voodoo Library

written by daniel on December 1st, 2008 @ 10:37 PM

So over a year ago I wanted a Ruby library that could do measurement conversions. Inches to centimeters, gallons to cups, anything. I got a bit carried away and abstracted all the units into yaml files, while going on a metaprogramming binge in the 179-line library.

I never released it, and don’t entirely remember why I wanted it in the first place, but Brandon asked me about it today, so I pulled it up, it worked, and I pushed it to GitHub.

It works pretty well, and I have a decent set of units:

> include Measurement::Length
> Inch.new(12).to_centimeters
=> #<Measurement::Length::Centimeter:0x173c2b4 @units=30.48>
>
> include Measurement::Volume
> Milliliter.new(100).to_teaspoons
=> #<Measurement::Volume::Teaspoon:0x1720e88 @units=45.0>

All the units (with conversions and abbreviations) are in yaml files:

Meter:
  abbreviation: m
  prefix: si
Foot:
  abbreviation: ft
  to_base: 0.3048
Inch:
  abbreviation: in
  parent: Foot
  to_parent: 1.0/12.0

And I’m having way too much fun with prefixes (mega-, centi-, etc.) in their own files:

mega:
  prefix: M
  base: 10
  exponent: 6
kilo:
  prefix: k
  base: 10
  exponent: 3
…
centi:
  prefix: c
  base: 10
  exponent: -2

I’m not really sure how this whole thing works anymore, but I’m having fun looking at it again. Tell me what to add, or fork it yourself and let’s measure. In the meantime, ponder why beer, wine and oil barrels are all different measurements (36, 31, 42 gallons, respectively).

Comments

  • Charles Waldman on 18 Dec 10:35

    Why not use the database file from the Unix “units” program? It has over 2000 units already defined, and its freely available – why reinvent this?

  • Georges on 17 Feb 16:33

    I must admit that it would be interesting to use the GNU units database and apply it to your plugins. I’ll have a look at that for training …

  • satelliteko on 22 Feb 16:52

    Very cool project. Ill clone it now from Github!

    Thanks

Post a comment

Options:

Size

Colors