【发布时间】:2013-11-18 03:03:47
【问题描述】:
我有一个哈希数组,称为images,看起来像这样:
[{ area: 10, id: 39393, score: 10}, { area: 20, id: 33434, score: 5}, ...]
我想将每个对象的分数以百分比增加到最大区域。所以,这就是我现在的做法:
max_area = images.max_by { |el| el["score"] }["area"]
images.each do |image|
if image["area"] > 0
image["score"] += image["area"] / max_area
end
end
还有其他 Ruby 方法可以做到这一点吗?
【问题讨论】:
-
我很喜欢你的...
-
Same here.. 如果几乎所有分数都大于零,则使用它可能会更好,除非,但现在我们在这里谈论品味。
image["score"] += (image["area"]/max_area) unless image["area"] <= 0 -
我在考虑也许做一个地图,而不是 image.each...但是谢谢大家。
-
你的看起来不错。
map在这里会很丑。 -
是的..我在@Linuxios
标签: ruby arrays hash enumerable