I’m currently working on a sort of cookbook rails application for my wife, who is in culinary school, and just for fun. I’ve just started, but I’m feeling fairly ambitious with plans for searchable and rateable recipes, automatic food costing, meal planning and maybe nutrition information. Heck, maybe I’ll even open source it or sell it as a subscription service.
However, I’ve barely begun and quickly run into a problem: how to deal with all those crazy imperial units! Pounds or lbs, ounces or ozs, floz, cups, quarts and everything else with weird abbreviations and conversion systems that suck. Of course, like most problems, somebody else out there already has a great solution. Kevin Olbrich at SciWerks.com has created Ruby-Units.
gem install ruby-units
At first I wasn’t sure I liked the way it used strings to create Units, but after a bit of use I really started to dig it, especially when I saw you could put in mishmash units like happens so often in cooking.
"4lbs 6oz".unit # => 4.375 lbs
"4pounds 6oz".unit # => 4.375 lbs
Also very cool and helpful to me was the ability to check to see if units were compatible with each other.
"2 cup".unit.compatible? "qt".unit # => true
"2 cup".unit.compatible? "lb".unit # => false
One of my only complaints/problems with this so far is that I don’t see a built in method to return all compatible units. If there was one, I imagine that it would work like this:
"2 cup".unit.compatible_units # => ["tbsp", "tsp", "pint", "qt", "gal"] etc
Also I may need to at some point build in my own conversions for specific food types. For example, 1 gallon of water = 8.33 pounds. I’m not sure how easy these sort of conversions will be to implement.
There are a few other unit libraries for ruby that I found. There’s a package simply called Units that doesn’t look like it’s active anymore, but it did have some documentation right up front about adding conversion types. There’s also the Ruby Facets libraries, but I haven’t looked through them much yet. They only mention SI units, so I don’t know if they support Imperial units.