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)

我该怎么办?

4

3 回答 3

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"] 
于 2013-07-10T08:32:45.997 回答
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 在他们的解决方案中所做的那样加入它们。

于 2013-07-10T09:13:41.767 回答
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方法中 Array 没有keys方法。所以试试上面的代码让它可行。

于 2013-07-10T08:32:08.363 回答