有没有在 Rails 上转换测量单位的解决方案?

发布于 2024-09-05 04:25:35 字数 151 浏览 11 评论 0原文

我想在 Ruby on Rails 应用程序中实现测量单位首选项。

例如,用户应该能够选择以英里或公里为单位显示距离。显然,不仅显示,还输入值。

我认为所有值都应该存储在一个全局测量系统中以简化计算。

有没有任何解决方案?或者我应该自己写?

I'd like to implement measurement unit preferences in a Ruby on Rails app.

For instance, the user should be able to select between displaying distances in miles or in kilometers. And, obviously, not only displaying, but entering values, too.

I suppose all values should be stored in one global measurement system to simplify calculations.

Are there any drop-in solutions for this? Or should I maybe write my own?

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

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

发布评论

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

评论(4

故人的歌 2024-09-12 04:25:35

红宝石宝石“ruby-units”可能会有所帮助:

http://ruby-units.rubyforge.org /红宝石单位/

require 'rubygems'
require 'ruby-units'

'8.4 mi'.to('km')      # => 13.3576 km
'8 lb 8 oz'.to('kg')   # => 3.85554 kg

a = '3 in'.to_unit
b = Unit('5 cm')
a + b                  # => 4.968 in
(a + b).to('cm')       # => 16.62 cm

The ruby gem "ruby-units" may help:

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

require 'rubygems'
require 'ruby-units'

'8.4 mi'.to('km')      # => 13.3576 km
'8 lb 8 oz'.to('kg')   # => 3.85554 kg

a = '3 in'.to_unit
b = Unit('5 cm')
a + b                  # => 4.968 in
(a + b).to('cm')       # => 16.62 cm
烟凡古楼 2024-09-12 04:25:35

您也许可以看看这个 gem,它可以让您执行一些单位转换。

Github 上的数量

You can maybe have a look at this gem, which let's you perform some unit conversions.

Quantity on Github

七婞 2024-09-12 04:25:35

我构建了 Unitwise 来解决 Ruby 中的大多数单位转换和测量数学问题。

简单的用法如下所示:

require 'unitwise/ext'

26.2.mile.convert_to('km') 
# => #<Unitwise::Measurement 42.164897129794255 kilometer>

如果您想在 Rails 模型中存储测量值,您可以执行以下操作:

class Race < ActiveRecord::Base
  # Convert value to kilometer and store the number
  def distance=(value)
    super(value.convert_to("kilometer").to_f)
  end

  # Convert the database value to kilometer measurement when retrieved
  def distance
    super.convert_to('kilometer')
  end
end

# Then you could do
five_k = Race.new(distance: 5)
five_k.distance
# => #<Unitwise::Measurement 5 kilometer>

marathon = Race.new(distance: 26.2.mile)
marathon.distance.convert_to('foot')
# => #<Unitwise::Measurement 138336.27667255333 foot>

I built Unitwise to solve most unit conversion and measurement math problems in Ruby.

Simple usage looks like this:

require 'unitwise/ext'

26.2.mile.convert_to('km') 
# => #<Unitwise::Measurement 42.164897129794255 kilometer>

If you want store measurements in your Rails models, you could do something like this:

class Race < ActiveRecord::Base
  # Convert value to kilometer and store the number
  def distance=(value)
    super(value.convert_to("kilometer").to_f)
  end

  # Convert the database value to kilometer measurement when retrieved
  def distance
    super.convert_to('kilometer')
  end
end

# Then you could do
five_k = Race.new(distance: 5)
five_k.distance
# => #<Unitwise::Measurement 5 kilometer>

marathon = Race.new(distance: 26.2.mile)
marathon.distance.convert_to('foot')
# => #<Unitwise::Measurement 138336.27667255333 foot>
亣腦蒛氧 2024-09-12 04:25:35

在 GitHub 上快速搜索发现了这个: http://github.com/collectiveidea/measurement

听起来像这样做你需要的事情(就单位之间的转换而言),但我不能说我自己使用过它。

编辑:皮埃尔的宝石看起来更坚固、更活跃。

Quick search on GitHub turned up this: http://github.com/collectiveidea/measurement

Sounds like it does what you need (as far as converting between units), but I can't say I've used it myself.

Edit: Pierre's gem looks like it's more robust and active.

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