初始化一個哈希裏的數組我試圖初始化數組這樣的哈希作爲我怎麼能在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