]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/lib/puppet/parser/functions/zip.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / zip.rb
1 #
2 # zip.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:zip, :type => :rvalue, :doc => <<-EOS
7 Takes one element from first array and merges corresponding elements from second array. This generates a sequence of n-element arrays, where n is one more than the count of arguments.
8
9 *Example:*
10
11     zip(['1','2','3'],['4','5','6'])
12
13 Would result in:
14
15     ["1", "4"], ["2", "5"], ["3", "6"]
16     EOS
17   ) do |arguments|
18
19     # Technically we support three arguments but only first is mandatory ...
20     raise(Puppet::ParseError, "zip(): Wrong number of arguments " +
21       "given (#{arguments.size} for 2)") if arguments.size < 2
22
23     a = arguments[0]
24     b = arguments[1]
25
26     unless a.is_a?(Array) and b.is_a?(Array)
27       raise(Puppet::ParseError, 'zip(): Requires array to work with')
28     end
29
30     flatten = function_str2bool([arguments[2]]) if arguments[2]
31
32     result = a.zip(b)
33     result = flatten ? result.flatten : result
34
35     return result
36   end
37 end
38
39 # vim: set ts=2 sw=2 et :