gpt4 book ai didi

ruby - 用于在 Ruby 中转换物理单位(千克、磅)的库?

转载 作者:数据小太阳 更新时间:2023-10-29 06:47:44 24 4
gpt4 key购买 nike

我知道 Rails 有一些用于日期和时间管理的好 helper 。是否有一个已经在运行的项目包含或合并了用于标准单位及其转换的干净的 DSL?任何具有在两个系统的基本长度和重量单位之间进行转换的助手的项目都可以工作。谢谢!

最佳答案

哇,比我想象的要多很多。这是我认为的完整列表,按字母顺序排列,示例用法从他们的文档中删除。我还注意到我是否无法让它们在本地运行。

  1. Alchemist

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

    这个看起来已经过时了,最后一次提交是 5 年前的,对我来说它似乎不能在 1.9.3 上运行。

    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

    请注意,这个声明在 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

    参见 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

    与另一位发帖人一样,我无法正常工作。不过使用方法可以看Jörg W Mittag's answer

作为 Unitwise 的作者,很明显它是我最喜欢的。不过,我鼓励您至少检查一下。

关于ruby - 用于在 Ruby 中转换物理单位(千克、磅)的库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4235764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com