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

Ruby access hash

I have the following hash:

{"title"=>"ga:browser=Firefox", "dimensions"=>[{:browser=>"Firefox"}], "metrics"=>[{:pageviews=>25474}], "id"=>"http://www.google.com/analytics/feeds/data?ids=ga:3906565&amp;ga:browser=Firefox&amp;start-date=2010-02-06&amp;end-date=2011-02-06", "updated"=>#<DateTime: 212163710400001/86400000,-1/3,2299161>} 

How do I print pageviews?

+7
source share
1 answer

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

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

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

+14
source

All Articles