1 module Puppet::Parser::Functions
2 newfunction(:merge, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
3 Merges two or more hashes together and returns the resulting hash.
7 $hash1 = {'one' => 1, 'two', => 2}
8 $hash2 = {'two' => 'dos', 'three', => 'tres'}
9 $merged_hash = merge($hash1, $hash2)
10 # The resulting hash is equivalent to:
11 # $merged_hash = {'one' => 1, 'two' => 'dos', 'three' => 'tres'}
13 When there is a duplicate key, the key in the rightmost hash will "win."
18 raise Puppet::ParseError, ("merge(): wrong number of arguments (#{args.length}; must be at least 2)")
21 # The hash we accumulate into
22 accumulator = Hash.new
23 # Merge into the accumulator hash
25 next if arg.is_a? String and arg.empty? # empty string is synonym for puppet's undef
26 unless arg.is_a?(Hash)
27 raise Puppet::ParseError, "merge: unexpected argument type #{arg.class}, only expects hash arguments"
29 accumulator.merge!(arg)
31 # Return the fully merged hash