Update puppetlabs/stdlib module
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / difference.rb
1 #
2 # difference.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:difference, :type => :rvalue, :doc => <<-DOC
6     This function returns the difference between two arrays.
7     The returned array is a copy of the original array, removing any items that
8     also appear in the second array.
9
10     *Examples:*
11
12         difference(["a","b","c"],["b","c","d"])
13
14     Would return: ["a"]
15
16     Note: Since Puppet 4 the minus (-) operator in the Puppet language does the same thing:
17
18       ['a', 'b', 'c'] - ['b', 'c', 'd']
19       # would return ['a']
20
21     DOC
22              ) do |arguments|
23
24     # Two arguments are required
25     raise(Puppet::ParseError, "difference(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size != 2
26
27     first = arguments[0]
28     second = arguments[1]
29
30     unless first.is_a?(Array) && second.is_a?(Array)
31       raise(Puppet::ParseError, 'difference(): Requires 2 arrays')
32     end
33
34     result = first - second
35
36     return result
37   end
38 end
39
40 # vim: set ts=2 sw=2 et :