-1

我的代码是这个

@logs= {-1=>{""=>{:source=>1, :time=>0, :skipped=>0, :mysql=>0, :es=>1}}}
@logs.each_pair { |user_id , user_content|
  user_content.each_pair { |kwd , ser_content.each_pair { |kwd , kwd_content|
  h=kwd_content
}}

 h={:time_taken=>0, :skipped_count=>0, :mysql_count=>0, :es_count=>1, :source_count=>1}

任何其他方式都需要很多时间,请帮助我

4

1 回答 1

0

如果您正在寻找的是对哈希值的值的哈希值求和:

@logs = {-1=>{""=>{:source=>1, :time=>0, :skipped=>0, :mysql=>0, :es=>1}}, -2=>{""=>{:source=>1, :time=>0, :skipped=>0, :mysql=>0, :es=>1}}}
# last part is from http://stackoverflow.com/a/4453690/178651
h = @logs.values.map{|i|i.values}.flatten.inject{|memo, el| memo.merge( el ){|k, old_v, new_v| old_v + new_v}}

将产生以下 h 值:

=> {:source=>2, :time=>0, :skipped=>0, :mysql=>0, :es=>2}
于 2013-05-21T18:24:15.727 回答