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

发布于 2024-10-03 05:01:32 字数 112 浏览 4 评论 0原文

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

余罪 2024-10-10 05:01:32

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

  1. 炼金术士

    需要“炼金术士”
    炼金术士设置
    8. 英里 为 米
    10.公里 + 1.英里 # 11.609344 公里
    
  2. 测量

    这个似乎已经过时了,最后一次提交是 5 年前,而且它似乎无法在我的 1.9.3 上运行。

    包括测量::长度
    英寸.new(12).to_厘米 # => #<测量::长度::厘米:0x173c2b4 @units=30.48>
    
  3. 物理单位

    需要“phys/units”
    Q = 物理::数量
    U = 物理::单位
    
    Q[1.23,'公里'] + Q[4.56,'米'] #=>物理::数量[1.23456,'公里']
    Q[123,'英里'] / Q[2,'小时'] #=> Phys::数量[61,'英里/小时']
    Q[61,'miles/hr'].want('m/s') #=>物理::数量[27.26944,'m/s']
    Q[1.0,'are'] == Q[10,'m']**2 #==>真的
    
  4. 数量

    请注意,自述文件中指出它尚未准备好用于生产。

    要求“数量/全部”
    1.米#=> 1米
    1.meter.to_feet #=> 3.28083...脚
    c = 299792458.米/1.秒#=> 299792458米/秒
    
    牛顿 = 1.米 * 1.公斤 / 1.秒**2 #=> 1米*公斤/秒^2
    newton.to_feet #=> 3.28083989501312 英尺*千克/秒^2
    newton.convert(:脚) #=> 3.28083989501312 英尺*千克/秒^2
    jerk_newton = 牛顿 / 1.second #=> 1米*公斤/秒^3
    jerk_newton * 1.second == 牛顿#=>真的
    
  5. SY

    请参阅Boris Stitnickey 的回答

  6. ruby-measurement

    需要“ruby-measurement”
    
    measurement =Measurement.parse('3 英尺') # => 3.0 英尺
    measurement.convert_to(:yards) # =>; 1.0 码。
    测量.convert_to(:in) # => 36.0 英寸
    measurement.convert_to(:英寸) # => 36.0 英寸
    
    measurement1 =Measurement.parse('3 英尺') # => 3.0 英尺
    measurement2 =Measurement.parse('6 英寸') # => 6.0 英寸
    测量1 + 测量2 # => 3.5 英尺
    
  7. ruby-units

    需要“ruby-units”
    
    unit = Unit("mm") # 仅单位(默认为标量 1)
    unit = Unit("1 mm") # 创建一个简单单位
    unit = Unit("1 kg*m^2/s^2") # 复杂单位
    
    unit1 =~ unit2 # 如果单位兼容则为 true
    unit1.兼容?(unit2) # 如果单位兼容则为 true
    
    单位1=单位>> "ft" # 转换为 '英尺'
    unit3 =unit1+unit2#生成的对象将具有unit1的单位
    
    unit1 === unit2 # 如果单位和数量相同,则为 true,即使通过 <=> '等效'
    unit.convert_to('ft') # 转换
    (unit1 + unit2).convert_to('ft') # 将结果转换为 'ft'
    
  8. Ruby 单位

    需要“单位”
    
    三米 = 3.米
    两米 = 2.m
    一英寸 = 1.英寸
    
    3.meters.to_inches # => 118.1103英寸
    10.英寸.to_mm # => 254.0毫米
    
  9. Unitwise

    需要“unitwise/ext”
    
    1.yard == (1.foot * 3) # =>真的
    
    1.夸脱< 1.升# =>真的
    
    2.米 + 3.英寸 - 1.码 # => <单位::测量1.1618米>
    
    1.convert_to('kg.m2/s2') == 1.joule # =>真的
    
    (20.kg * 9.8.convert_to('m/s2')).to_pound_force # => <按单位::测量 44.06255284754326 pound_force>
    
    (300.马力 * 60.秒).to_calorie # => <单位::测量3208077.8414151203卡路里>
    
  10. 货车/单元

    像另一位海报一样,我在实现此功能时遇到了困难。不过,您可以查看 Jörg W Mittag 的回答以了解用法。

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

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.

冷清清 2024-10-10 05:01:32

尝试一下很棒的Alchemist

......

Try awesome Alchemist.

......

谁把谁当真 2024-10-10 05:01:32

看看 Tom Sawyer 的 Van/Unit

这是来自主页:

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

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
骷髅 2024-10-10 05:01:32

尝试 gem install sy

require 'sy'

( 5.g *                # a bullet of 5g
    500.m.s(-1) ** 2 / # flying at a speed of 500m/s has kJ energy:
2 ).in( :kJ )          #=> 0.625

require 'sy/imperial'    

1.mile.in :ft   #=> 5280.0
1.acre.in :yd²  #=> 4840.0
1.gallon.in :l  #=> 4.54609
1.lb.in :kg     #=> 0.45359237
1.mile².in :km² #=> 2.589988110336
SY::SPEED_OF_LIGHT.in "furlong.fortnight⁻¹" #=> 1802617499785.2544

Try gem install sy

require 'sy'

( 5.g *                # a bullet of 5g
    500.m.s(-1) ** 2 / # flying at a speed of 500m/s has kJ energy:
2 ).in( :kJ )          #=> 0.625

require 'sy/imperial'    

1.mile.in :ft   #=> 5280.0
1.acre.in :yd²  #=> 4840.0
1.gallon.in :l  #=> 4.54609
1.lb.in :kg     #=> 0.45359237
1.mile².in :km² #=> 2.589988110336
SY::SPEED_OF_LIGHT.in "furlong.fortnight⁻¹" #=> 1802617499785.2544
梦醒灬来后我 2024-10-10 05:01:32

我无法让货车/单位工作 - 它所报告的只是:

/Library/Ruby/Gems/1.8/gems/vanunits-1.5.0/lib/van/units/base.rb:493:in
‘to’:不兼容的操作单位
(类型错误)
来自unitstest.rb:4

运行时,

require 'rubygems' 
require 'van/units' 
include Van::Units puts
1.meter.to(feet)

我花了一个小时尝试破解van/units 的源,但没有成功。

它所基于的旧版 gem 开箱即用:

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

I was unable to get Van/units to work - all it reports is:

/Library/Ruby/Gems/1.8/gems/vanunits-1.5.0/lib/van/units/base.rb:493:in
`to': incompatible units for operation
(TypeError)
from unitstest.rb:4

When running

require 'rubygems' 
require 'van/units' 
include Van::Units puts
1.meter.to(feet)

I spent an hour unsuccessfully attempting to hack the source of van/units.

The older gem on which it is based worked out of the box:

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

初懵 2024-10-10 05:01:32

vanunits 似乎坏了。我建议尝试

ruby​​units (http://ruby-units.rubyforge.org/ruby-units/)

测量 (https://github.com/collectiveidea/measurement)

vanunits seems broken. i suggest to try

rubyunits (http://ruby-units.rubyforge.org/ruby-units/)

or

measurements (https://github.com/collectiveidea/measurement)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文