用于转换Ruby中物理单位(千克,磅)的库?

Jos*_*man 9 ruby dsl physics units-of-measurement

我知道Rails有一些很好的日期和时间管理助手.是否有一个已经完成的项目由标准单元及其转换组成或包含干净的DSL?任何带有助手的项目都可以在两个系统的基本长度和重量单位之间进行转换.谢谢!

Jos*_*wis 20

哇,这些比我预期的要多得多.以下是我认为的完整列表,按字母顺序排列,示例用法从文档中删除.我还注意到,如果我无法让它们在本地运行.

  1. 方士

    require 'alchemist'
    Alchemist.setup
    8.miles.to.meters
    10.kilometers + 1.mile # 11.609344 kilometers
    
    Run Code Online (Sandbox Code Playgroud)
  2. 测量

    这个看起来已经过时了,最后一次提交是5年前的事,而且它似乎并没有在1.9.3上运行.

    include Measurement::Length
    Inch.new(12).to_centimeters # => #<Measurement::Length::Centimeter:0x173c2b4 @units=30.48>
    
    Run Code Online (Sandbox Code Playgroud)
  3. 物理学单位

    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
    
    Run Code Online (Sandbox Code Playgroud)
  4. 数量

    请注意,这个说明它在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
    
    Run Code Online (Sandbox Code Playgroud)
  5. SY

    请参阅Boris Stitnickey的回答.

  6. 红宝石测量

    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.
    
    Run Code Online (Sandbox Code Playgroud)
  7. 红宝石单位

    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'
    
    Run Code Online (Sandbox Code Playgroud)
  8. 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
    
    Run Code Online (Sandbox Code Playgroud)
  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>
    
    Run Code Online (Sandbox Code Playgroud)
  10. 货车/股

    像另一张海报一样,我无法正常工作.但是,您可以看到JörgWMittag的使用答案.

作为Unitwise的作者,可能很明显它是我最喜欢的群体.不过,我鼓励你至少检查一下.


Cha*_*tni 6

尝试令人敬畏的炼金术士.

......