-1

My code is 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 will takes much time any another way for this please help me

MrDanA
  • 11,489
  • 2
  • 36
  • 47
Ravendra Kumar
  • 1,072
  • 10
  • 29
  • 2
    I'm not sure what you are doing there - the keys of the final hash are different from the ones you started with - and i'm not sure of all cases you may have, but i suspect Hash.flatten may help you. – theglauber May 21 '13 at 13:46

1 Answers1

0

If what you are looking for is to total the hash values of the values of the values of the 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 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}}

would result in the following value of h:

=> {:source=>2, :time=>0, :skipped=>0, :mysql=>0, :es=>2}
Gary S. Weaver
  • 7,966
  • 4
  • 37
  • 61