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
Post a Comment