在Ruby中向哈希添加变量

时间:2013-05-22 01:31:22

标签: ruby hash

我需要将一个变量添加到Hash中。我试图这样做:

actorsChickens = Hash.new
nicolasCage = gets.chomp # is a string
chickenCount = gets.chomp # is a string representing a number (i.e "5")
actorsChickens[#{nicolasCage} => #{chickenCount}]

我做错了什么?如何将变量添加到哈希?

1 个答案:

答案 0 :(得分:4)

我认为你正在尝试这样做:

your_hash = {}
hash_key = gets.chomp
hash_value = gets.chomp
your_hash[hash_key] = hash_value