source: other-projects/hathitrust/vagrant-hadoop-cluster/trunk/modules/stdlib/lib/puppet/parser/functions/merge.rb@ 30903

Last change on this file since 30903 was 30903, checked in by davidb, 7 years ago

Vagrant provisioning files for a 4-node Hadoop cluster. See README.txt for more details

File size: 1.1 KB
Line 
1module Puppet::Parser::Functions
2 newfunction(:merge, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
3 Merges two or more hashes together and returns the resulting hash.
4
5 For example:
6
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'}
12
13 When there is a duplicate key, the key in the rightmost hash will "win."
14
15 ENDHEREDOC
16
17 if args.length < 2
18 raise Puppet::ParseError, ("merge(): wrong number of arguments (#{args.length}; must be at least 2)")
19 end
20
21 # The hash we accumulate into
22 accumulator = Hash.new
23 # Merge into the accumulator hash
24 args.each do |arg|
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"
28 end
29 accumulator.merge!(arg)
30 end
31 # Return the fully merged hash
32 accumulator
33 end
34end
Note: See TracBrowser for help on using the repository browser.