【问题标题】:How to sum up values from all hashes? [closed]如何总结所有哈希值? [关闭]
【发布时间】:2015-12-06 22:22:30
【问题描述】:

如何比我更优雅地总结所有哈希值? boards_statistics 数组包含 items_info 哈希值。

def generate_accumulated_statistics(boards_statistics)
  # Create hash with zero values - its purprose is to
  # accumulate results from all other hashes
  resulted_hash = {
    items_info: {
      finished_items: {
        todo: 0,
        in_progress: 0,
        done: 0
      },

      hours_worked: {
        estimated: 0,
        time_logged: 0
      },

      story_points_completed: {
        estimated: 0,
        completed: 0
      },

      due_today_items: 0,
      late_items: 0
    },
    team_info: []
  }

  boards_statistics.each do |statistics|
    resulted_hash[:items_info][:finished_items][:todo] += statistics[:items_info][:finished_items][:todo]
    resulted_hash[:items_info][:finished_items][:in_progress] += statistics[:items_info][:finished_items][:in_progress]
    resulted_hash[:items_info][:finished_items][:done] += statistics[:items_info][:finished_items][:done]

    resulted_hash[:items_info][:hours_worked][:estimated] += statistics[:items_info][:hours_worked][:estimated]
    resulted_hash[:items_info][:hours_worked][:time_logged] += statistics[:items_info][:hours_worked][:time_logged]

    resulted_hash[:items_info][:story_points_completed][:estimated] += statistics[:items_info][:story_points_completed][:estimated]
    resulted_hash[:items_info][:story_points_completed][:completed] += statistics[:items_info][:story_points_completed][:completed]

    resulted_hash[:items_info][:due_today_items] += statistics[:items_info][:due_today_items]
    resulted_hash[:items_info][:late_items] += statistics[:items_info][:late_items]
  end
end

【问题讨论】:

  • 我投票结束这个问题作为题外话,因为询问如何做一些更优雅的工作是一个代码审查,属于Code Review而不是Stack Overflow,这是用于代码问题。
  • 快速:resulted_hash.to_s.scan(/=>\K\d+/).reduce(0) { |tot, s| tot + s.to_i } #=> 0。全部为零并不是最好的例子。 “result_hash”或“resulting_hash”会是一个更好的名字,因为“resulted”是一个动词。

标签: ruby algorithm ruby-on-rails-4 hash


【解决方案1】:

您可以实现自己的深度合并风格。这里:

class ::Hash
  def deep_merge(second)
    merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : (v1+v2) }
    self.merge(second, &merger)
  end
end

resulted_hash
# => {:items_info=>{:finished_items=>{:todo=>0, :in_progress=>0, :done=>0}, :hours_worked=>{:estimated=>0, :time_logged=>0}, :story_points_completed=>{:estimated=>0, :completed=>0}, :due_today_items=>0, :late_items=>0}, :team_info=>[]}

boards_statistics
#  => {:items_info=>{:finished_items=>{:todo=>1, :in_progress=>1, :done=>1}, :hours_worked=>{:estimated=>1, :time_logged=>1}, :story_points_completed=>{:estimated=>1, :completed=>1}, :due_today_items=>1, :late_items=>1}, :team_info=>[]}

resulted_hash = resulted_hash.deep_merge(boards_statistics)
# => {:items_info=>{:finished_items=>{:todo=>1, :in_progress=>1, :done=>1}, :hours_worked=>{:estimated=>1, :time_logged=>1}, :story_points_completed=>{:estimated=>1, :completed=>1}, :due_today_items=>1, :late_items=>1}, :team_info=>[]}

resulted_hash = resulted_hash.deep_merge(boards_statistics)
# => {:items_info=>{:finished_items=>{:todo=>2, :in_progress=>2, :done=>2}, :hours_worked=>{:estimated=>2, :time_logged=>2}, :story_points_completed=>{:estimated=>2, :completed=>2}, :due_today_items=>2, :late_items=>2}, :team_info=>[]}

【讨论】:

    猜你喜欢
    • 2013-10-11
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2014-04-12
    • 2011-10-24
    • 2020-05-11
    相关资源
    最近更新 更多