fork download
  1. #
  2. # status・training 以外のも追加されたうえ
  3. # key/valueが増えたのにも対処するならこんな感じ?
  4. #
  5. status = {'hp' => 100,'mp' => 20}
  6. training = {'hp' => 15,'mp' => 20}
  7. another = {'hp' => 1,'mp' => 1}
  8. hey_guys = {'hp' => 2,'mp' => 2}
  9.  
  10. result = {}
  11.  
  12. status.each_key{|key| result[key] = 0 }
  13.  
  14. [status, training, another, hey_guys].each {|item|
  15. result.each_key{|key|
  16. result[key] += item[key]
  17. }
  18. }
  19.  
  20. raise "assert" if result['hp'] != 118
  21. raise "assert" if result['mp'] != 43
  22.  
  23. p result
Success #stdin #stdout 0s 28688KB
stdin
Standard input is empty
stdout
{"hp"=>118, "mp"=>43}