计量单位转换宝石
对于我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想尝试 ruby-units:
您可以检查 单元定义 看看这个是否适合您!
You might want to try ruby-units:
You can check the unit definitions to see if this one suits you !
您可以使用 Unitwise 来完成此操作。它专为大量科学单位的单位转换和测量数学而设计。它不了解 Rails,但连接起来应该相当容易:
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: