Retrieve nested hash values ruby -


i have nested hash :

{   :a=>{        :toto=>{                :foo=>10,                :bar=>11,                :baz=>12               },        :titi=>"a"      },   :b=>{        :toto=>{                :foo=>31,                :bar=>45,                :baz=>78               },        :titi=>"b"      } } 

my goal sum :baz values. i'm sure there beautiful way in ruby. idea?

thanks.

#inject powerful method works both arrays , hashes. can walk through values of hash , sum needed key total sum.

hash.inject(0) { |sum, (_,v)| sum += v[:toto][:baz] } # => 90 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -