From: Logan Capaldo Date: 2007-05-04T23:44:45+09:00 Subject: Re: Beyond YAML? (scaling) ------=_Part_40511_27011418.1178289884174 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 5/4/07, Bil Kleb wrote: > > William James wrote: > > > > Would making a copy of the hash use too much > > memory or time? > > I don't know, but that's surely another way out of > the Marshal-hash-proc trap... > > Thanks, > -- > Bil Kleb > http://fun3d.larc.nasa.gov > > I dunno how safe it is to rely on this behavior, but calling Hash#default= strips the proc and makes the hash marshallable: >> h = Hash.new { |h,k| h[k] = [] } => {} >> h["a"] => [] >> h => {"a"=>[]} >> h["b"] = 7 => 7 >> h => {"a"=>[], "b"=>7} >> h.default = nil => nil >> h => {"a"=>[], "b"=>7} >> Marshal.dump h => "\004\b{\a\"\006a[\000\"\006bi\f" of course this means you won't get your default proc back when you load the hash and mutate it. However: db = Marshal.load( from_disk ) delta = Hash.new { |h, k| h[k] = if db.has_key? k then db[k] else [] end } delta["foo"] = "add a new value" delta["bar"] << "update something in the original" ... more of the same ... to_disk = Marshal.dump( db.merge!( delta ) ) ------=_Part_40511_27011418.1178289884174--