4 module Puppet::Parser::Functions
5 newfunction(:has_key, :type => :rvalue, :doc => <<-'DOC') do |args|
6 Determine if a hash has a certain key value.
10 $my_hash = {'key_one' => 'value_one'}
11 if has_key($my_hash, 'key_two') {
12 notice('we will not reach here')
14 if has_key($my_hash, 'key_one') {
15 notice('this will be printed')
18 Note: Since Puppet 4.0.0 this can be achieved in the Puppet language with the following equivalent expression:
20 $my_hash = {'key_one' => 'value_one'}
21 if 'key_one' in $my_hash {
22 notice('this will be printed')
26 unless args.length == 2
27 raise Puppet::ParseError, "has_key(): wrong number of arguments (#{args.length}; must be 2)"
29 unless args[0].is_a?(Hash)
30 raise Puppet::ParseError, "has_key(): expects the first argument to be a hash, got #{args[0].inspect} which is of type #{args[0].class}"