puppetlabs-stdlib/lib/puppet/parser/functions/intersection.rb
Alex Cline 737aa31546 (#20684) Add array comparison functions, difference, intersection and union.
Included is code, tests and documentation for the difference, intersection
and union functions for comparing arrays.
2013-05-13 12:14:15 -04:00

34 lines
728 B
Ruby

#
# intersection.rb
#
module Puppet::Parser::Functions
newfunction(:intersection, :type => :rvalue, :doc => <<-EOS
This function returns an array an intersection of two.
*Examples:*
intersection(["a","b","c"],["b","c","d"])
Would return: ["b","c"]
EOS
) 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')
end
result = first & second
return result
end
end
# vim: set ts=2 sw=2 et :