我想使用一个参数作为我的关键在哈希中查找值,我只是困惑为什么我不能通过第一种方式获取值。我是Ruby的新手。使用Ruby散列键作为参数
def getCards(player,hash)
a =$player
puts "a = "+a.to_s
puts "a.class = "+a.class.to_s
puts " hash[:a]"+" #{hash[:a]}"
puts " hash[:'1']"+" #{hash[:"1"]}"
end
编辑:
def getCards(player,hash)
puts player
#result successfully 1 or any number that I gets from console
puts hash[player]
# nothing but 1 is actually a key in my hash
# {1=>["yellow3", "yellow8", "green9", "black11", "red1", "black7", "red5", #"yellow7", more results ..
end
hash [“1”]返回我需要的结果,但是为什么我不能使用变量作为参数键来获取值? –