初始化一个哈希里的数组我试图初始化数组这样的哈希作为我怎么能在Ruby中
@my_hash = Hash.new(Array.new)
,这样我可以:
@my_hash["hello"].push("in the street")
=> ["in the street"]
@my_hash["hello"].push("at home")
=> ["in the street", "at home"]
@my_hash["hello"]
=>["in the street", "at home"]
的问题是,任何新散列键还返回["in the street", "at home"]
@my_hash["bye"]
=> ["in the street", "at home"]
@my_hash["xxx"]
=> ["in the street", "at home"]
!!! ???
我在做什么错误什么是正确的方法来初始化一个数组哈希?
相关:http://stackoverflow.com/questions/190740/setting-ruby-hash-default-to-a-list – tokland 2010-12-10 15:38:51