【发布时间】:2015-08-25 00:30:18
【问题描述】:
我正在尝试创建一个数组,该数组从其他数组中收集单个字符串,然后创建一个哈希来计算estimate.bottom 中包含的每个字符串的数量。底部belongs_to 估计。
我的问题是它当前不读取单个字符串,而是读取整个数组。因此,如果底部是 ["a", "b"] 、 ["a"] 和 ["b"] ,则哈希 bottom_count 将是 {["a"]=>1, ["b"]=>1, ["a", "b"]=>1} 而不是 {["a"]=>2, ["b"]=>2}
@in_bottom = []
@estimates.each do |est|
@in_bottom << est.bottom
end
@bottom_count = Hash.new(0)
@in_bottom.each { |in_bottom| @bottom_count[in_bottom] += 1 }
如何使该方法遍历各个字符串并使bottom_count 正常工作?
【问题讨论】:
-
用
@in_bottom << est.bottom.flatten替换@in_bottom << est.bottom
标签: ruby-on-rails arrays ruby ruby-on-rails-4 hash