# # File 'hash.rb' created on 09 ott 2007 at 15:59:11. # # See 'dokkit.rb' or LICENSE for licence information. # # (c)2006, 2007 Andrea Fazzi (and contributors). # class Hash # Set the default values for keyword arguments. # See http://www.lukeredpath.co.uk/2006/7/27/using-ruby-hashes-as-keyword-arguments-with-easy-defaults # for a complete discussion. def with_defaults!(defaults) self.merge!(defaults) { |key, old, new| old.nil? ? new : old } end # Non-desctructive version of Hash#with_defaults method. def with_defaults(defaults) self.merge(defaults) { |key, old, new| old.nil? ? new : old } end # Hash#recursive_merge merges two arbitrarily deep hashes into a single hash. # The hash is followed recursively, so that deeply nested hashes that are at # the same level will be merged when the parent hashes are merged. def recursive_merge!(other_hash) merge!(other_hash) do |key, value, other_value| if(value.class == Hash && other_value.class == Hash) value.recursive_merge other_value elsif(value.class == Array && other_value.class == Array) value.concat(other_value) else store(key, other_value) end end end # Non-destructive version of Hash#recursive_merge method. def recursive_merge(other_hash) dup.recursive_merge!(other_hash) end end