ruby on rails - Convert hash of hash into Single hash -
my code this
@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}
it takes time way please me
if looking total hash values of values of values of hash:
@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 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}}
would result in following value of h:
=> {:source=>2, :time=>0, :skipped=>0, :mysql=>0, :es=>2}
Comments
Post a Comment