access hashes of hashes values in ruby -
I have nested has been in Ruby and I need to reach its specific value. My hash resembles the bottom
hash = {"list" = & gt; {"0" = & gt; {"Date" = & gt; "11/03/2014", "item1" = & gt; "", "Tiem2" => "News", "Item 3" = & gt; "", "Item4" = & gt; "", "Item5" = & gt; "video", "type" => gt; clips}}, "1" => {"Date" = & gt; "11/03/2014", "item1" = & gt; "", "Tiem2" => "News", "Item 3" = & gt; "", "Item4" = & gt; "Each key needs to reach the value of" type ". I," item5 "= & gt;" video "," type "= & gt;" program "}}}
I tried with the code below, but I'm not sure why this does not work.
hash_type = hash ["list"]. Keys.each {| key | Key ["type"]}
but returned the list of keys. I.e. 0 and 1
please help.
hash ["list"] .map {| _, hash | hash ['type']}
< / Pre>Explanation:
hash = {key: 'value'}
on a hash by using
each
like this Can loop:hash.each {| pair | Puts pair.inspect} # = & gt; [: key, 'value']
Or it's like
hash {| key, value | puts} "# {key}: # {value}"} # = & gt; key: value
Since we do not use the key anywhere, some IDEs will complain about the unused local variable
key
. To prevent this, it is a Ruby Convention that they use_
for the variable name and all IDEs will not care about it being unused.
Comments
Post a Comment