Default default ruby ​​hash ruby ​​hash - delete default handler?

I have a hash with the default file, which I would like to make a marshal for the file, but by default this method does not allow me to do this.

Instead of writing my own _dump and _load , is it possible to instead remove proc by default instead? The moment I am Marshalling, I will no longer need the default process.

+11
ruby marshalling hash
Sep 29 '10 at 3:59 april
source share
2 answers

Just reset the default value:

 h.default = nil 

In details:

 def dumpable_hash(h) return h unless h.default_proc copy = h.clone copy.default = nil # clear the default_proc copy end 

In Ruby 2.0, you can also write h.default_proc = nil if you want. Available for all Rubies with require 'backports/2.0.0/hash/default_proc' .

+14
Sep 29 '10 at 4:15
source share

If you want to have a copy without a default, the easiest way is

 Hash[hash_with_defaults] 
+5
Jan 08 2018-12-12T00:
source share



All Articles