红宝石访问散列元素 [英] Ruby access hash element
本文介绍了红宝石访问散列元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下散列:
{"title"=>"ga:browser=Firefox", "dimensions"=>[{:browser=>"Firefox"}], "metrics"=>[{:pageviews=>25474}], "id"=>"http://www.google.com/analytics/feeds/data?ids=ga:3906565&ga:browser=Firefox&start-date=2010-02-06&end-date=2011-02-06", "updated"=>#<DateTime: 212163710400001/86400000,-1/3,2299161>}
我将如何打印浏览量的价值?
How would I print the value of pageviews?
推荐答案
好了,你有一个HashMap(不是数组),它映射键指标
来的数组。该数组包含哈希作为其唯一元素。而该散列映射键:浏览量
的值 25474
。因此,要获得该值,你可以做:
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]
这假定与散:浏览量
键将永远是在数组中的位置0,关键指标
映射到。
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.
这篇关于红宝石访问散列元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文