How can I marshal a hash with arrays?

前端 未结 4 1857
醉话见心
醉话见心 2021-01-24 04:23

What should I do to marshal an hash of arrays? The following code only prints {}.

s = Hash.new
s.default = Array.new
s[0] <<          


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-24 05:14

    As you can't use Marshall.dump on a Hash with a proc for the default element, you could use a slightly more roundabout way of appending to each Array instead of <<:

    s = Hash.new
    s.default = []
    s[0] += [ "Tigger" ]
    s[7] += [ "Ruth" ]
    s[7] += [ "Pooh" ]
    data = Marshal.dump(s)
    ls = Marshal.restore(data)
    p ls
    

提交回复
热议问题