我在红宝石中有嵌套哈希值,我需要访问它的特定值。我的哈希看起来像下面。在红宝石中访问哈希值散列值
hash =
{"list"=>
{"0"=>
{"date"=>"11/03/2014",
"item1"=>"",
"tiem2"=>"News",
"item3"=>"",
"item4"=>"",
"item5"=>"Videos",
"Type"=>"Clip"},
"1"=>
{"date"=>"11/03/2014",
"item1"=>"",
"tiem2"=>"News",
"item3"=>"",
"item4"=>"",
"item5"=>"Videos",
"Type"=>"Program"}
}}
我需要访问每个键的“类型”的值。 我试着用下面的代码,但我不知道为什么它没有工作。
hash_type = hash["list"].keys.each {|key| puts key["Type"]}
但它返回了密钥列表。即0和1
请帮忙。
由于您的键是字符串,这会工作:'h.to_s.scan(/(<=\"Type\"=> \”? )[^ \“] + /)=> [”剪辑“,”程序“](但我不会那样做)。 – 2014-11-06 05:25:57