我正在尝试为 RPG 编写角色创建器,但有两件事我有疑问。
- 如何检查字符串是否等于散列中的任何键,在这种情况下,是否
race_choice
等于 中的任何键races
? - 如何在不同的哈希中更改另一个键的值以等于包含等于我的字符串的键的键/值对,在这种情况下,如何使' 的值等于等于
player[race]
其的键/值对race_choice
钥匙?
这是我的代码的简化部分:
player = {
race: {}
}
races = {
human: {key1: "value", key2: "value"},
elf: {key1: "value", key2: "value"},
dwarf: {key1: "value", key2: "value"}
}
loop do
puts "What's your race?"
race_choice = gets.chomp.downcase
if race_choice == races[:race].to_s #I know this code doesn't work, but hopefully it
player[:race].store(races[:race, info]) #gives you a better idea of what I want to do.
puts "Your race is #{player[:race[:race]]}."
break
elsif race_choice.include?('random')
puts "Random race generation goes here!"
break
else
puts "Race not recognized!"
end
end
我希望玩家从种族列表中选择他们的种族,然后将该种族及其相关信息添加到玩家的角色中。