2013-07-10 59 views
0

我正在使用散列在Ruby中的散列,名为MYMOVIES,如下所示。现在散列哈希,如何获得第一个嵌套散列的键

{"127 Hours"=> 
     {"title"=>"127 Hours", 
     "year"=>"2010", 
     "plays"=>1, 
     "last_played"=>1300489200, 
     "seen_date"=>"19/3/2011", 
     "imdb_id"=>"tt1542344", 
     "rating"=>"6", 
     "omdbapiurl"=>"http://www.omdbapi.com/?t=127 Hours&y=2010"}, 
    "Zombieland"=> 
     {"title"=>"Zombieland", 
     "year"=>"2009", 
     "plays"=>1, 
     "last_played"=>1290207600, 
     "seen_date"=>"20/11/2010", 
     "imdb_id"=>"tt1156398", 
     "rating"=>"7", 
     "omdbapiurl"=>"http://www.omdbapi.com/?t=Zombieland&y=2009"}} 

,我想获得第一嵌套哈希的所有键(即名称,年份,播放,...,omdbapiurl)。

我试着用:

mynestedhash = MYMOVIES.first 
    puts mynestedhash.keys.to_s 

但我得到的错误:

undefined method `keys' for #<Array:0x801c56f8> (NoMethodError) 

我怎么能这样做?

回答

2

如果所有内部散列按键相同,下面就足够了

first_outer_key, first_outer_value = MYMOVIES.first 
first_inner_hash = first_outer_value # change name to show what we have 
inner_keys = first_inner_hash.keys 

如果内哈希键可以是不同的,你应该加入他们 像Priti和toro2k在他们的解决方案中做到了。

+0

太好了,非常感谢! – MDT

1
require 'pp' 
h = {"127 Hours"=> 
     {"title"=>"127 Hours", 
     "year"=>"2010", 
     "plays"=>1, 
     "last_played"=>1300489200, 
     "seen_date"=>"19/3/2011", 
     "imdb_id"=>"tt1542344", 
     "rating"=>"6", 
     "omdbapiurl"=>"http://www.omdbapi.com/?t=127 Hours&y=2010"}, 
    "Zombieland"=> 
     {"title"=>"Zombieland", 
     "year"=>"2009", 
     "plays"=>1, 
     "last_played"=>1290207600, 
     "seen_date"=>"20/11/2010", 
     "imdb_id"=>"tt1156398", 
     "rating"=>"7", 
     "omdbapiurl"=>"http://www.omdbapi.com/?t=Zombieland&y=2009"}} 

pp h.flat_map{|k,v| v.keys}.uniq 

输出

["title", 
"year", 
"plays", 
"last_played", 
"seen_date", 
"imdb_id", 
"rating", 
"omdbapiurl"] 

现在明白为什么你的代码没有工作如下:

h = {"127 Hours"=> 
     {"title"=>"127 Hours", 
     "year"=>"2010", 
     "plays"=>1, 
     "last_played"=>1300489200, 
     "seen_date"=>"19/3/2011", 
     "imdb_id"=>"tt1542344", 
     "rating"=>"6", 
     "omdbapiurl"=>"http://www.omdbapi.com/?t=127 Hours&y=2010"}, 
    "Zombieland"=> 
     {"title"=>"Zombieland", 
     "year"=>"2009", 
     "plays"=>1, 
     "last_played"=>1290207600, 
     "seen_date"=>"20/11/2010", 
     "imdb_id"=>"tt1156398", 
     "rating"=>"7", 
     "omdbapiurl"=>"http://www.omdbapi.com/?t=Zombieland&y=2009"}} 

h.first 
#["127 Hours", 
# {"title"=>"127 Hours", 
# "year"=>"2010", 
# "plays"=>1, 
# "last_played"=>1300489200, 
# "seen_date"=>"19/3/2011", 
# "imdb_id"=>"tt1542344", 
# "rating"=>"6", 
# "omdbapiurl"=>"http://www.omdbapi.com/?t=127 Hours&y=2010"}] 
p h.first.grep /keys/ 
#[] 

现在很明显,从#grep方法阵列没有keys方法。因此,请尝试上面的代码以使其可行。

+0

非常感谢您的有用解释! – MDT

2

这应该这样做:

MYMOVIES.map { |_, h| h.keys }.flatten.uniq 
# => ["title", "year", "plays", "last_played", "seen_date", "imdb_id", "rating", "omdbapiurl"] 

您的代码没有工作,因为first返回一个数组,而不是一个哈希方法:

MYMOVIES.first 
# => ["127 Hours", {"title"=>"127 Hours", ... }]] 

更新如果你想拿到钥匙你可以这样做:

nested_hash = MYMOVIES.first[1] 
nested_hash.keys 
# => ["title", "year", "plays", "last_played", "seen_date", "imdb_id", "rating", "omdbapiurl"] 

或者:

_, nested_hash = MYMOVIES.first 
nested_hash.keys 
# => ["title", "year", "plays", "last_played", "seen_date", "imdb_id", "rating", "omdbapiurl"] 
+0

是的,但没有办法从散列中返回散列(因为我将它创建为散列散列!)? mytmplist = {} mytmplist [ '标题'] = “” mytmplist [ '年'] = “” mytmplist [ '起着'] = 1个 mytmplist [ 'last_played'] = mytimestamp mytmplist [ 'seen_date' ] = myformattedseendate mytmplist [ 'imdb_id'] = “” mytmplist [ '等级'] = “” mytmplist [ 'omdbapiurl'] = “” MYMOVIES [movietitle] = “” – MDT

+2

@MDT你在这个岗位要求是什么已被回答。现在,如果你有其他问题需要发布在另一个问题。从评论它是不可读的,你在找什么。 –

+0

'mytmplist = {} mytmplist [ '标题'] = “” mytmplist [ '年'] = “” mytmplist [ '起着'] = 1个 mytmplist [ 'last_played'] = mytimestamp mytmplist [ 'seen_date' ] = myformattedseendate mytmplist [ 'imdb_id'] = “” mytmplist [ '等级'] = “” mytmplist [ 'omdbapiurl'] = “” MYMOVIES [movietitle] = mytmplist' 这就是我想后,对此之前不可读的帖子感到抱歉,并非常感谢您的有用答案。 – MDT