Puppet Function: delete_values

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

Overview

delete_values()Hash

> Note: Since Puppet 4.0.0 the equivalent can be performed with the built-in [`filter`](puppet.com/docs/puppet/latest/function.html#filter) function: $array.filter |$val| { $val != 'B' } $hash.filter |$key, $val| { $val != 'B' }

Examples:

Example usage


delete_values({'a'=>'A','b'=>'B','c'=>'C','B'=>'D'}, 'B')
Would return: {'a'=>'A','c'=>'C','B'=>'D'}

Returns:

  • (Hash)

    The given hash now missing all instances of the targeted value



5
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 'modules/stdlib/lib/puppet/parser/functions/delete_values.rb', line 5

newfunction(:delete_values, :type => :rvalue, :doc => <<-DOC
  @summary
    Deletes all instances of a given value from a hash.

  @example Example usage

    delete_values({'a'=>'A','b'=>'B','c'=>'C','B'=>'D'}, 'B')
    Would return: {'a'=>'A','c'=>'C','B'=>'D'}

  > *Note:*
  Since Puppet 4.0.0 the equivalent can be performed with the
  built-in [`filter`](https://puppet.com/docs/puppet/latest/function.html#filter) function:
  $array.filter |$val| { $val != 'B' }
  $hash.filter |$key, $val| { $val != 'B' }

  @return [Hash] The given hash now missing all instances of the targeted value
  DOC
           ) do |arguments|

  raise(Puppet::ParseError, "delete_values(): Wrong number of arguments given (#{arguments.size} of 2)") if arguments.size != 2

  hash, item = arguments

  unless hash.is_a?(Hash)
    raise(TypeError, "delete_values(): First argument must be a Hash. Given an argument of class #{hash.class}.")
  end
  hash.dup.delete_if { |_key, val| item == val }
end