简体   繁体   中英

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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