def weave(h)
raise ArgumentError, "Hash expected" unless h.kind_of?(Hash)
s = self.clone
h.each { |k,node|
node_is_hash = node.kind_of?(Hash)
node_is_array = node.kind_of?(Array)
if s.has_key?(k)
self_node_is_hash = s[k].kind_of?(Hash)
self_node_is_array = s[k].kind_of?(Array)
if self_node_is_hash
if node_is_hash
s[k] = s[k].weave(node)
elsif node_is_array
raise ArgumentError, 'Incompatible hash addition'
else
raise ArgumentError, 'Incompatible hash addition'
end
elsif self_node_is_array
if node_is_hash
raise ArgumentError, 'Incompatible hash addition'
elsif node_is_array
s[k] += node
else
s[k] << node
end
else
if node_is_hash
raise ArgumentError, 'Incompatible hash addition'
elsif node_is_array
s[k].unshift( node )
else
s[k] = node
end
end
else
s[k] = node
end
}
s
end