From: Pete Elmore Date: 2005-04-02T10:28:12+09:00 Subject: Two Questions about Hash Defaults I have a couple of questions about hashes. The first is fairly simple: is there a way to set a default so that when the value for a key is changed, the object it points to is default.dup instead of default? So that if I do something like this: lists = Hash.new([]) lists[:a].push 1 lists[:b].push 2 # Then doing the following p lists[:a] # Gets me => [1] # Instead of => [1, 2] And 'lists' is '{:a => [1], :b => [2]}' instead of '{}' with a default of '[1, 2]'. I know why it happens this way, but is it possible to get it to behave differently? So that instead of lists[key] = [] if lists[key].nil? # Assuming default is nil lists[key].push value or lists[key] = lists[key].dup.push value # default is [] I can write simply lists[key].push value # default is [] The second question is about setting defaults from one hash based on another. This question is more along the lines of 'Does this code violate Ruby sensibilities?' class Hash def add_defaults(default_hash) raise TypeError unless default_hash.kind_of? Hash default_hash.each { |k,v| self[k] = v unless self.has_key? k } self end end def some_function(str, options=Hash.new(nil)) # Set the defaults defaults = { :preserve_breaks => true, :indent => 0, :columns => 79, } options.add_defaults defaults end some_function("asdf", { :preserve_breaks => false })