Puppet Function: difference

Defined in:
vendor_modules/stdlib/lib/puppet/parser/functions/difference.rb
Function type:
Ruby 3.x API

Summary

This function returns the difference between two arrays.

Overview

difference()Array

The returned array is a copy of the original array, removing any items that also appear in the second array.

> Note: Since Puppet 4 the minus (-) operator in the Puppet language does the same thing:

'a', 'b', 'c'
  • 'b', 'c', 'd'

Would return: `['a']`

Examples:

Example usage


difference(["a","b","c"],["b","c","d"])
Would return: `["a"]`

Returns:

  • (Array)

    The difference between the two given arrays



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'vendor_modules/stdlib/lib/puppet/parser/functions/difference.rb', line 7

newfunction(:difference, type: :rvalue, doc: <<-DOC
  @summary
    This function returns the difference between two arrays.

  The returned array is a copy of the original array, removing any items that
  also appear in the second array.

  @example Example usage

    difference(["a","b","c"],["b","c","d"])
    Would return: `["a"]`

  > *Note:*
  Since Puppet 4 the minus (-) operator in the Puppet language does the same thing:
  ['a', 'b', 'c'] - ['b', 'c', 'd']
  Would return: `['a']`

  @return [Array]
    The difference between the two given arrays

  DOC
) do |arguments|
  # Two arguments are required
  raise(Puppet::ParseError, "difference(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size != 2

  first = arguments[0]
  second = arguments[1]

  unless first.is_a?(Array) && second.is_a?(Array)
    raise(Puppet::ParseError, 'difference(): Requires 2 arrays')
  end

  result = first - second

  return result
end