Puppet Function: map_hash

Defined in:
lib/puppet/parser/functions/map_hash.rb
Function type:
Ruby 3.x API

Overview

map_hash()Any

This function maps something onto each element of a hash.

Examples:

later ...

Will return: nothing …

Returns:

  • (Any)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/puppet/parser/functions/map_hash.rb', line 6

newfunction(:map_hash, :type => :rvalue, :doc => <<-EOS
This function maps something onto each element of a hash.

*Examples:*

  later ...

Will return: nothing ...
  EOS
) do |arguments|
  raise(Puppet::ParseError, "map_hash(): Wrong number of arguments " +
        "(given #{arguments.size} for 2)") if arguments.size < 2

  hash = arguments[0]

  unless hash.is_a?(Hash)
    raise Puppet::ParseError, "map_hash(): expected first argument to be a Hash, got #{hash.inspect}"
  end

  puppet_string = arguments[1]

  # sanity check for arg[1]

  result = hash.inject({}) { |h, (key,value)| h[key] = puppet_string % [ key, value ]; h }

  return result
end