Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / hash.rb
1 #
2 # hash.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:hash, :type => :rvalue, :doc => <<-EOS
7 This function converts an array into a hash.
8
9 *Examples:*
10
11     hash(['a',1,'b',2,'c',3])
12
13 Would return: {'a'=>1,'b'=>2,'c'=>3}
14     EOS
15   ) do |arguments|
16
17     raise(Puppet::ParseError, "hash(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1
18
19     array = arguments[0]
20
21     unless array.is_a?(Array)
22       raise(Puppet::ParseError, 'hash(): Requires array to work with')
23     end
24
25     result = {}
26
27     begin
28       # This is to make it compatible with older version of Ruby ...
29       array  = array.flatten
30       result = Hash[*array]
31     rescue StandardError
32       raise(Puppet::ParseError, 'hash(): Unable to compute hash from array given')
33     end
34
35     return result
36   end
37 end
38
39 # vim: set ts=2 sw=2 et :