我有两个散列:

p = {"name"=>"TRICHI", "subdistrict"=>{"WANDIWASH"=>"1234"}}
q = {"name"=>"VELLORE", "subdistrict"=>{"WANDIWASH"=>"4183"}}

我要把它变成
r = [{"name"=>"VELLORE", "subdistrict"=>{"WANDIWASH"=>"4183"}},
    {"name"=>"TRICHI", "subdistrict"=>{"WANDIWASH"=>"1234"}}]

最佳答案

我想你想要这个:

r = [] << p << q
# or r = [p, q]
# either way you'll get:
#  [ {"name"=>"VELLORE", "subdistrict"=>{"WANDIWASH"=>"4183"}},
#    {"name"=>"TRICHI", "subdistrict"=>{"WANDIWASH"=>"1234"}}  ]

这样你就会有一个array和2hashes

关于ruby-on-rails - 如何在ruby中的现有哈希中插入新哈希,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9582185/

10-13 02:50