Report abuse

1
2
3
4
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
32
33
34
35
36
37
#
# prefix.rb
#

module Puppet::Parser::Functions
  newfunction(:intersect, :type => :rvalue, :doc => <<-EOS
This function Intersects two arrays.

*Examles:*

    intersect(['a','b','c'], ['p','a','b])

Will return: ['a','b']
    EOS
  ) do |arguments|

    # Technically we support two arguments but only first is mandatory ...
    raise(Puppet::ParseError, "intersect(): Wrong number of arguments " +
      "given (#{arguments.size} for 1)") if arguments.size < 2

    array1 = arguments[0]

    unless array1.is_a?(Array)
      raise(Puppet::ParseError, 'intersect(): Requires array to work with')
    end

    array2 = arguments[1] if arguments[1]

    unless array2.is_a?(Array)
        raise(Puppet::ParseError, 'intersect(): Requires array to work with')
      end

    # Turn everything into string same as join would do ...
 
    return array1 & array2
  end
end