Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / join.rb
1 #
2 # join.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:join, :type => :rvalue, :doc => <<-EOS
7 This function joins an array into a string using a separator.
8
9 *Examples:*
10
11     join(['a','b','c'], ",")
12
13 Would result in: "a,b,c"
14     EOS
15   ) do |arguments|
16
17     # Technically we support two arguments but only first is mandatory ...
18     raise(Puppet::ParseError, "join(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1
19
20     array = arguments[0]
21
22     unless array.is_a?(Array)
23       raise(Puppet::ParseError, 'join(): Requires array to work with')
24     end
25
26     suffix = arguments[1] if arguments[1]
27
28     if suffix
29       unless suffix.is_a?(String)
30         raise(Puppet::ParseError, 'join(): Requires string to work with')
31       end
32     end
33
34     result = suffix ? array.join(suffix) : array.join
35
36     return result
37   end
38 end
39
40 # vim: set ts=2 sw=2 et :