简体   繁体   English

Ruby-如何从嵌套在哈希中的数组访问键

[英]Ruby - How do I access keys from an array nested in a hash

I'm working on a small video game on the console where I have a main config hash that stores values for the game. 我正在控制台上开发一款小型视频游戏,在这里我有一个主要配置哈希值,用于存储游戏的值。 When I try to access keys in an array in that hash, they don't return anything. 当我尝试访问该哈希中的数组中的键时,它们不返回任何内容。 Am I doing something wrong? 难道我做错了什么?

cfg =
{
    :gameVersion => 1.0,
    :invPouch => ['flint', 'string'],
    :gold => 50
}

puts cfg[:invPouch[1]]

It should be: 它应该是:

puts cfg[:invPouch][1]

In your case, :invPouch[1] is "n" , and cfg["n"] gives you nil . 在您的情况下, :invPouch[1]"n" ,而cfg["n"]nil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM