I have an assignment that I cannot figure out where my mistake lies. I have a large array of hashes all under the method twitter_data. The hash is structured as such.
我有一项任务,我无法弄清楚我的错误在哪里。我在twitter_data方法下有大量的哈希值。哈希的结构如此。
def twitter_data
[{"User"=>
{"description"=>
"Description here",
"last twenty tweets"=>
["tweets written out here"],
"number of followers"=>1000,
"number of friends"=>100,
"latest tweet"=>
"tweet written out here",
"number of tweets"=>1000,
"location"=>"Wherever, Wherever"}},]
end
Now if I wanted to for instance list all of the users and their descriptions I thought the code would read as such.
现在,如果我想列举所有用户及其描述,我认为代码会这样读。
twitter_data.each do |twitter_data|
puts "#{twitter_data[:twitter_data]}: #{twitter_data[:description]}"
end
But the output for that just gives me about seven :, without the username in front of it or the description afterwards.
但是那个输出只给了我七个:没有前面的用户名或之后的描述。
2 个解决方案
#1
0
As you can see the description
key is nested into another hash which key is User
. I don't know which is the other key you want to print because data seems incomplete but if you wanted to print just the descriptions this one should work
正如您所看到的,描述键嵌套在另一个哈希中,其中键是User。我不知道哪个是您要打印的另一个键,因为数据似乎不完整但是如果您只想打印描述这个应该可以工作
twitter_data.each do |user_data|
description = user_data["User"]["description"]
puts description
end
#2
0
There are a couple of reasons why this does not work:
有几个原因导致这不起作用:
1) The twitter_data
element inside the each
looks like this { 'User' => { 'description'...
. On that hash, the value stored under the :description
key is nil.
1)每个内部的twitter_data元素看起来像这样{'User'=> {'description'....在该哈希值上,存储在:description键下的值为nil。
2) Even if you where to refer to the correct hash via twitter_data['User']
you would still be using symbols (e.g. :description
) instead of strings. So even then, the value stored for the keys would be nil.
2)即使您通过twitter_data ['User']在哪里引用正确的哈希,您仍然会使用符号(例如:description)而不是字符串。即便如此,为密钥存储的值也将为零。
3) You are referencing elements that do not seem to exist in the hash even if one where to use strings (e.g. :twitter_data
). Now this might simply be due to the example selected.
3)您正在引用哈希中似乎不存在的元素,即使在哪里使用字符串(例如:twitter_data)。现在这可能仅仅是由于选择了示例。
What will work is to correctly reference the hashes:
什么工作是正确引用哈希:
twitter_data.each do |data|
user_hash = data['User']
puts "#{user_hash['twitter_data']}: #{user_hash['description']}"
end
#1
0
As you can see the description
key is nested into another hash which key is User
. I don't know which is the other key you want to print because data seems incomplete but if you wanted to print just the descriptions this one should work
正如您所看到的,描述键嵌套在另一个哈希中,其中键是User。我不知道哪个是您要打印的另一个键,因为数据似乎不完整但是如果您只想打印描述这个应该可以工作
twitter_data.each do |user_data|
description = user_data["User"]["description"]
puts description
end
#2
0
There are a couple of reasons why this does not work:
有几个原因导致这不起作用:
1) The twitter_data
element inside the each
looks like this { 'User' => { 'description'...
. On that hash, the value stored under the :description
key is nil.
1)每个内部的twitter_data元素看起来像这样{'User'=> {'description'....在该哈希值上,存储在:description键下的值为nil。
2) Even if you where to refer to the correct hash via twitter_data['User']
you would still be using symbols (e.g. :description
) instead of strings. So even then, the value stored for the keys would be nil.
2)即使您通过twitter_data ['User']在哪里引用正确的哈希,您仍然会使用符号(例如:description)而不是字符串。即便如此,为密钥存储的值也将为零。
3) You are referencing elements that do not seem to exist in the hash even if one where to use strings (e.g. :twitter_data
). Now this might simply be due to the example selected.
3)您正在引用哈希中似乎不存在的元素,即使在哪里使用字符串(例如:twitter_data)。现在这可能仅仅是由于选择了示例。
What will work is to correctly reference the hashes:
什么工作是正确引用哈希:
twitter_data.each do |data|
user_hash = data['User']
puts "#{user_hash['twitter_data']}: #{user_hash['description']}"
end