如何访问哈希数组中的哈希值?
我有一个标题为 entities
的哈希数组,
下面是该数组的外观:
p "entities"
y entities
p "entities[0]"
y entities[0]
p "entities[1]"
y entities[1]
p "entities[2]"
y entities[2]
输出:
"entities"
--- |
---
- :text: Berlin
:links:
- http://dbpedia.org/resource/Berlin
- :text: Jan Karski
:links:
- http://dbpedia.org/resource/Jan_Karski
- :text: God
:links:
- http://dbpedia.org/resource/God
"entities[0]"
--- "-"
"entities[1]"
--- "-"
"entities[2]"
--- "-"
我不知道如何访问各个哈希值。当我执行 pEntity[0]
时,应该会出现什么情况吗?
I have an array of hashes titled entities
Here is a look at the array:
p "entities"
y entities
p "entities[0]"
y entities[0]
p "entities[1]"
y entities[1]
p "entities[2]"
y entities[2]
Output:
"entities"
--- |
---
- :text: Berlin
:links:
- http://dbpedia.org/resource/Berlin
- :text: Jan Karski
:links:
- http://dbpedia.org/resource/Jan_Karski
- :text: God
:links:
- http://dbpedia.org/resource/God
"entities[0]"
--- "-"
"entities[1]"
--- "-"
"entities[2]"
--- "-"
I can't figure out how to access the individual hashes. Should something be coming up when I do p entities[0]
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是entities.kind_of吗?字符串 正确吗?看起来实体实际上是一个 YAML 字符串。因此,entities[0/1/2] 是 YAML 的前三个破折号。
Is
entities.kind_of? String
true? It looks like entities is actually a YAML string. Hence, entities[0/1/2] are the first three dashes of the YAML.抄对了吗?粘贴到 irb 会话中的输出作为单个字符串而不是数组出现:
Did that copy right? the output, pasted into a irb session comes out as a single string, not an array:
您的
entitles
可能并不是真正的哈希数组。看这个:
尝试
entitles.class
和entitles[0].class
Your
entitles
may be not really array of hashes.Look this:
Try
entitles.class
andentitles[0].class