Ruby access hash element

前端 未结 1 1265
一向
一向 2021-02-09 09:06

I have the following hash:

{\"title\"=>\"ga:browser=Firefox\", \"dimensions\"=>[{:browser=>\"Firefox\"}], \"metrics\"=>[{:pageviews=>25474}], \"id         


        
相关标签:
1条回答
  • 2021-02-09 09:22

    Well, you have a hashmap (not an array) which maps the key "metrics" to an array. That array contains a hash as its only element. And that hash maps the key :pageviews to the value 25474. So to get that value you can do:

    the_hash["metrics"][0][:pageviews]
    

    This assumes that the hash with the :pageviews key will always be at position 0 in the array, which the key "metrics" is mapped to.

    0 讨论(0)
提交回复
热议问题