Puppet Function: array_intersect

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

Overview

array_intersect()Any

Given two arrays, returns all elements that exist in both.

Returns:

  • (Any)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/puppet/parser/functions/array_intersect.rb', line 5

newfunction(:array_intersect, :type => :rvalue, :doc => <<-EOS
Given two arrays, returns all elements that exist in both.
EOS
) do |arguments|
  raise(Puppet::ParseError, "array_intersect(): Wrong number of arguments " +
    "given (#{arguments.size} for 2)") if arguments.size < 2

  lhs = Array(arguments[0])
  rhs = Array(arguments[1])

  unless lhs.is_a?(Array)
    raise(Puppet::ParseError, 'array_intersect(): expected array for parameter 1')
  end

  unless rhs.is_a?(Array)
    raise(Puppet::ParseError, 'array_intersect(): expected array for paraemter 2')
  end

  result = lhs & rhs
  return result
end