Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libraries for converting physical units (kilograms, pounds) in Ruby?

I know Rails has some nice helpers for date and time management. Is there an already-working project consisting of, or incorporating a clean DSL for standard units and their conversions? Any project with helpers for the conversion between the two systems' basic units of length and weight would work. Thank you!

like image 702
Joseph Weissman Avatar asked Nov 21 '10 00:11

Joseph Weissman


3 Answers

Wow, there are quite a few more of these than I expected. Here is what I believe to be a complete list, in alphabetical order, with example usage stripped from their documentation. I also noted if I couldn't get them running locally.

  1. Alchemist

    require 'alchemist'
    Alchemist.setup
    8.miles.to.meters
    10.kilometers + 1.mile # 11.609344 kilometers
    
  2. measurement

    This one appears out of date, the last commit was 5 years ago, and it doesn't seem to run on 1.9.3 for me.

    include Measurement::Length
    Inch.new(12).to_centimeters # => #<Measurement::Length::Centimeter:0x173c2b4 @units=30.48>
    
  3. Phys-Units

    require "phys/units"
    Q = Phys::Quantity
    U = Phys::Unit
    
    Q[1.23,'km'] + Q[4.56,'m']    #=> Phys::Quantity[1.23456,'km']
    Q[123,'mile'] / Q[2,'hr']     #=> Phys::Quantity[61,'mile/hr']
    Q[61,'miles/hr'].want('m/s')  #=> Phys::Quantity[27.26944,'m/s']
    Q[1.0,'are'] == Q[10,'m']**2  #=> true
    
  4. Quantity

    Note that this one states that it is not production ready in the README.

    require 'quantity/all'
    1.meter                                         #=> 1 meter
    1.meter.to_feet                                 #=> 3.28083... foot
    c = 299792458.meters / 1.second                 #=> 299792458 meter/second
    
    newton = 1.meter * 1.kilogram / 1.second**2     #=> 1 meter*kilogram/second^2
    newton.to_feet                                  #=> 3.28083989501312 foot*kilogram/second^2
    newton.convert(:feet)                                   #=> 3.28083989501312 foot*kilogram/second^2
    jerk_newton = newton / 1.second                         #=> 1 meter*kilogram/second^3
    jerk_newton * 1.second == newton                        #=> true
    
  5. SY

    See Boris Stitnickey's answer.

  6. ruby-measurement

    require 'ruby-measurement'
    
    measurement = Measurement.parse('3 feet')  # => 3.0 ft.
    measurement.convert_to(:yards)             # => 1.0 yd.
    measurement.convert_to(:in)                # => 36.0 in.
    measurement.convert_to(:inches)            # => 36.0 in.
    
    measurement1 = Measurement.parse('3 feet') # => 3.0 ft.
    measurement2 = Measurement.parse('6 inch') # => 6.0 in.
    measurement1 + measurement2                # => 3.5 ft.
    
  7. ruby-units

    require 'ruby-units'
    
    unit = Unit("mm")                # unit only (defaults to a scalar of 1)
    unit = Unit("1 mm")              # create a simple unit
    unit = Unit("1 kg*m^2/s^2")      # complex unit
    
    unit1 =~ unit2                   # true if units are compatible
    unit1.compatible?(unit2)         # true if units are compatible
    
    unit1 = unit >> "ft"             # convert to 'feet'
    unit3 = unit1 + unit2            # resulting object will have the units of unit1
    
    unit1 === unit2                  # true if units and quantity are the same, even if 'equivalent' by <=>
    unit.convert_to('ft')            # convert
    (unit1 + unit2).convert_to('ft') # converts result to 'ft'
    
  8. Units for Ruby

    require 'units'
    
    three_meters = 3.meters
    two_meters = 2.m
    one_inch = 1.inch
    
    3.meters.to_inches # => 118.1103 inch
    10.inches.to_mm    # => 254.0 millimeter
    
  9. Unitwise

    require 'unitwise/ext'
    
    1.yard == (1.foot * 3)                       # => true
    
    1.quart < 1.liter                            # => true
    
    2.meter + 3.inch - 1.yard                    # => <Unitwise::Measurement 1.1618 meter>
    
    1.convert_to('kg.m2/s2') == 1.joule          # => true
    
    (20.kg * 9.8.convert_to('m/s2')).to_pound_force # => <Unitwise::Measurement 44.06255284754326 pound_force>
    
    (300.horsepower * 60.second).to_calorie      # => <Unitwise::Measurement 3208077.8414151203 calorie>
    
  10. Van/Unit

    Like another poster, I had trouble getting this working. However, you can see Jörg W Mittag's answer for usage.

As the author of Unitwise, it might be obvious that it's my favorite out of the group. I encourage you to at least check it out, though.

like image 81
Josh W Lewis Avatar answered Nov 14 '22 05:11

Josh W Lewis


Try awesome Alchemist.

......

like image 30
Chandra Patni Avatar answered Nov 14 '22 06:11

Chandra Patni


Take a look at Tom Sawyer's Van/Unit

This is from the homepage:

require 'van/units'
include Units

1.mile.to(feet)
1.acre.to(yd**2)
1.acre.to(sq_yd)
1.gallon.to(self.L)
1.lb.to(kg)
1.m.s.to(m.s)
1.sq_mi.to(km**2)
1.mile.to(km)
1.usd.to(twd)
1.bit/s + 8.bytes/s
like image 21
Jörg W Mittag Avatar answered Nov 14 '22 04:11

Jörg W Mittag