我想使用一個參數作爲我的關鍵在哈希中查找值,我只是困惑爲什麼我不能通過第一種方式獲取值。我是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」]返回我需要的結果,但是爲什麼我不能使用變量作爲參數鍵來獲取值? –