Update stdlib and concat to 6.1.0 both
[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     @summary
7       This function returns the difference between two arrays.
8
9     The returned array is a copy of the original array, removing any items that
10     also appear in the second array.
11
12     @example Example usage
13
14       difference(["a","b","c"],["b","c","d"])
15       Would return: `["a"]`
16
17     > *Note:*
18     Since Puppet 4 the minus (-) operator in the Puppet language does the same thing:
19     ['a', 'b', 'c'] - ['b', 'c', 'd']
20     Would return: `['a']`
21
22     @return [Array]
23       The difference between the two given arrays
24
25     DOC
26              ) do |arguments|
27
28     # Two arguments are required
29     raise(Puppet::ParseError, "difference(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size != 2
30
31     first = arguments[0]
32     second = arguments[1]
33
34     unless first.is_a?(Array) && second.is_a?(Array)
35       raise(Puppet::ParseError, 'difference(): Requires 2 arrays')
36     end
37
38     result = first - second
39
40     return result
41   end
42 end
43
44 # vim: set ts=2 sw=2 et :