source: other-projects/hathitrust/vagrant-hadoop-cluster/trunk/modules/stdlib/lib/puppet/parser/functions/hash.rb@ 30903

Last change on this file since 30903 was 30903, checked in by davidb, 7 years ago

Vagrant provisioning files for a 4-node Hadoop cluster. See README.txt for more details

File size: 864 bytes
Line 
1#
2# hash.rb
3#
4
5module Puppet::Parser::Functions
6 newfunction(:hash, :type => :rvalue, :doc => <<-EOS
7This function converts an array into a hash.
8
9*Examples:*
10
11 hash(['a',1,'b',2,'c',3])
12
13Would return: {'a'=>1,'b'=>2,'c'=>3}
14 EOS
15 ) do |arguments|
16
17 raise(Puppet::ParseError, "hash(): Wrong number of arguments " +
18 "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, 'hash(): Requires array to work with')
24 end
25
26 result = {}
27
28 begin
29 # This is to make it compatible with older version of Ruby ...
30 array = array.flatten
31 result = Hash[*array]
32 rescue StandardError
33 raise(Puppet::ParseError, 'hash(): Unable to compute ' +
34 'hash from array given')
35 end
36
37 return result
38 end
39end
40
41# vim: set ts=2 sw=2 et :
Note: See TracBrowser for help on using the repository browser.