计量单位转换宝石

发布于 2024-11-15 10:49:40 字数 881 浏览 3 评论 0原文

对于我的 Rails 项目,我正在寻找一个可以转换质量、体积和其他单位的库。

我需要将千克转换为克,升转换为汤匙等。

我认为,它应该看起来像这样:

class Product < ActiveRecord:Base
  acts_as_physical_unit, :volume, :mass, :count
end

class Ingredient < ActiveRecord:Base
  acts_as_physical_unit, :volume, :mass, :count
end

olive_oil = Product.new(:name => "Olive Oil", :volume => "1000 ml")

caesar_salad = Recipe.new(:name => "Caesar salad",
  :ingredients => [
    Ingredient.new(:product => [olive_oil], :volume => "5 tablespoons")
  ]

# In ingredients of "Caesar Salad" are 5 tablespoons of "Olive Oil" specified.
# We should create "Caesar Salad" for 50 persons.
# How mutch bottles of "Olive Oil" should be ordered ?
order = OrderItem.new(
  :product => olive_oil,
  :count => olive_oil.count_for(caesar_salad.ingredients.first)) * 50
)

这样的宝石是否存在?

谢谢。

for my Rails project, I'm looking for a library, that can convert mass, volume and other units.

I need to convert kilogramms to gramms, liters to tablespoons etc.

I think, it should look like this:

class Product < ActiveRecord:Base
  acts_as_physical_unit, :volume, :mass, :count
end

class Ingredient < ActiveRecord:Base
  acts_as_physical_unit, :volume, :mass, :count
end

olive_oil = Product.new(:name => "Olive Oil", :volume => "1000 ml")

caesar_salad = Recipe.new(:name => "Caesar salad",
  :ingredients => [
    Ingredient.new(:product => [olive_oil], :volume => "5 tablespoons")
  ]

# In ingredients of "Caesar Salad" are 5 tablespoons of "Olive Oil" specified.
# We should create "Caesar Salad" for 50 persons.
# How mutch bottles of "Olive Oil" should be ordered ?
order = OrderItem.new(
  :product => olive_oil,
  :count => olive_oil.count_for(caesar_salad.ingredients.first)) * 50
)

Does such a gem even exists ?

Thanks.

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

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

发布评论

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

评论(2

绻影浮沉 2024-11-22 10:49:40

您可能想尝试 ruby-units

您可以检查 单元定义 看看这个是否适合您!

You might want to try ruby-units:

You can check the unit definitions to see if this one suits you !

强辩 2024-11-22 10:49:40

您可以使用 Unitwise 来完成此操作。它专为大量科学单位的单位转换和测量数学而设计。它不了解 Rails,但连接起来应该相当容易:

require 'unitwise/ext'

class Ingredient < ActiveRecord::Base
  # store the value as milliliter in the database
  def volume=(amount)
    super(amount.convert_to('ml').to_f)
  end

  # convert the value to a measurement when retrieved
  def volume
    super().convert_to('ml')
  end
end

# Now you can initialize by sending a number (assumed to be in ml)
cooking_wine = Ingredient.new(name: 'Cooking Wine', volume: 750)

# Or send any other volume measurement
olive_oil = Ingredient.new(name: 'Olive Oil', volume: 1.liter)
cumin = Ingredient.new(name: 'Cumin', volume: 5.teaspoon)

# Now volume returns a measurement
cooking_wine.volume # => #<Unitwise::Measurement 750.0 ml>

# And the volume can be converted to whatever you want.
olive_oil.volume.convert_to('gallon') # => #<Unitwise::Measurement 0.21996924829908776 gallon>

# Use .to_f to get just the numeric value
cumin.volume.convert_to('cup').to_f # => 0.10416666666666666

# You can perform whatever math you need on the volumes as well:
2.0 * (olive_oil.volume + cooking_wine.volume) => #<Unitwise::Measurement 3500.0 ml>

You could do this with Unitwise. It's designed for unit conversion and measurement math for a ton of scientific units. It has no knowledge of Rails, but it should be fairly easy to wire up:

require 'unitwise/ext'

class Ingredient < ActiveRecord::Base
  # store the value as milliliter in the database
  def volume=(amount)
    super(amount.convert_to('ml').to_f)
  end

  # convert the value to a measurement when retrieved
  def volume
    super().convert_to('ml')
  end
end

# Now you can initialize by sending a number (assumed to be in ml)
cooking_wine = Ingredient.new(name: 'Cooking Wine', volume: 750)

# Or send any other volume measurement
olive_oil = Ingredient.new(name: 'Olive Oil', volume: 1.liter)
cumin = Ingredient.new(name: 'Cumin', volume: 5.teaspoon)

# Now volume returns a measurement
cooking_wine.volume # => #<Unitwise::Measurement 750.0 ml>

# And the volume can be converted to whatever you want.
olive_oil.volume.convert_to('gallon') # => #<Unitwise::Measurement 0.21996924829908776 gallon>

# Use .to_f to get just the numeric value
cumin.volume.convert_to('cup').to_f # => 0.10416666666666666

# You can perform whatever math you need on the volumes as well:
2.0 * (olive_oil.volume + cooking_wine.volume) => #<Unitwise::Measurement 3500.0 ml>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文