Puppet Function: intersection

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

Summary

This function returns an array of the intersection of two.

Overview

intersection()Any

Examples:

Example Usage:

intersection(["a","b","c"],["b","c","d"])  # returns ["b","c"]
intersection(["a","b","c"],[1,2,3,4])      # returns [] (true, when evaluated as a Boolean)

Returns:

  • (Any)

    an array of the intersection of two.



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
# File 'vendor_modules/stdlib/lib/puppet/parser/functions/intersection.rb', line 7

newfunction(:intersection, type: :rvalue, doc: <<-DOC
  @summary
    This function returns an array of the intersection of two.

  @return
    an array of the intersection of two.

  @example Example Usage:
    intersection(["a","b","c"],["b","c","d"])  # returns ["b","c"]
    intersection(["a","b","c"],[1,2,3,4])      # returns [] (true, when evaluated as a Boolean)
  DOC
) do |arguments|
  # Two arguments are required
  raise(Puppet::ParseError, "intersection(): 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, "intersection(): Requires 2 arrays, got #{first.class} and #{second.class}")
  end

  result = first & second

  return result
end