Puppet Function: similar_elements

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

Overview

similar_elements()Any

Given two arrays, returns an array with all elements that are in common

Returns:

  • (Any)


5
6
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
# File 'lib/puppet/parser/functions/similar_elements.rb', line 5

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

  inner = Array(arguments[0])
  outer = Array(arguments[1])

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

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

  result = [ ]
  inner.each do |i|
    if outer.include? i
      result.push i
    end
  end

  return result
end