10

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

4

6 回答 6

21

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

  1. 炼金术士

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

    这个似乎过时了,最后一次提交是 5 年前,对我来说它似乎没有在 1.9.3 上运行。

    include Measurement::Length
    Inch.new(12).to_centimeters # => #<Measurement::Length::Centimeter:0x173c2b4 @units=30.48>
    
  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
    
  4. 数量

    请注意,这表明它在自述文件中尚未准备好生产。

    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 的回答

  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.
    
  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'
    
  8. 红宝石单位

    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. 逐个

    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. 货车/单位

    像另一张海报一样,我很难让它发挥作用。但是,您可以查看Jörg W Mittag 的使用答案。

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

于 2014-02-15T07:08:32.363 回答
6

试试真棒炼金术士

……

于 2010-11-21T00:25:50.753 回答
1

尝试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
于 2013-05-18T17:52:01.510 回答
1

看看汤姆索亚的货车/单位

这是来自主页:

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
于 2010-11-21T00:36:53.590 回答
0

我无法让 Van/units 工作 - 它报告的只是:

/Library/Ruby/Gems/1.8/gems/vanunits-1.5.0/lib/van/units/base.rb:493:in `to': 来自unitstest.rb:4 的操作单元不兼容(TypeError)

跑步时

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

我花了一个小时试图破解货车/单位的来源,但没有成功。

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

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

于 2011-01-15T19:46:04.860 回答
0

vanunits似乎坏了。我建议尝试

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

或者

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

于 2011-02-17T17:28:56.883 回答