views:

39

answers:

3

I'd like to implement measurement unit preferences in a Ruby on Rails app.

For instance, the user should be able to select between displaying distances in miles or in kilometers. And, obviously, not only displaying, but entering values, too.

I suppose all values should be stored in one global measurement system to simplify calculations.

Are there any drop-in solutions for this? Or should I maybe write my own?

A: 

Quick search on GitHub turned up this: http://github.com/collectiveidea/measurement

Sounds like it does what you need (as far as converting between units), but I can't say I've used it myself.

Edit: Pierre's gem looks like it's more robust and active.

rspeicher
+1  A: 

Hi, you can maybe have a look at this gem, which let's you perform some unit conversions.

Quantity on Github

hth

P.

Pierre
+2  A: 

The ruby gem "ruby-units" may help:

http://ruby-units.rubyforge.org/ruby-units/

require 'rubygems'
require 'ruby-units'

'8.4 mi'.to('km')      # => 13.3576 km
'8 lb 8 oz'.to('kg')   # => 3.85554 kg

a = '3 in'.to_unit
b = Unit('5 cm')
a + b                  # => 4.968 in
(a + b).to('cm')       # => 16.62 cm
Justin L.