Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / intersection.rb
1 #
2 # intersection.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:intersection, :type => :rvalue, :doc => <<-DOC
6     @summary
7       This function returns an array of the intersection of two.
8
9     @return
10       an array of the intersection of two.
11
12     @example Example Usage:
13       intersection(["a","b","c"],["b","c","d"])  # returns ["b","c"]
14       intersection(["a","b","c"],[1,2,3,4])      # returns [] (true, when evaluated as a Boolean)
15     DOC
16              ) do |arguments|
17
18     # Two arguments are required
19     raise(Puppet::ParseError, "intersection(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size != 2
20
21     first = arguments[0]
22     second = arguments[1]
23
24     unless first.is_a?(Array) && second.is_a?(Array)
25       raise(Puppet::ParseError, 'intersection(): Requires 2 arrays')
26     end
27
28     result = first & second
29
30     return result
31   end
32 end
33
34 # vim: set ts=2 sw=2 et :