Puppet Function: compact_hash

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

Overview

compact_hash()Any

compact_hash

Compresses a hash to remove all elements whose values are nil or undef.

Examples


$example =

'one'  => 'two',
'red'  => undef,
'blue' => nil,

compact_hash($example) # => { ‘one => ’two’ }

Returns:

  • (Any)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/puppet/parser/functions/compact_hash.rb', line 1

Puppet::Parser::Functions.newfunction(:compact_hash,
                                      type: :rvalue,
                                      arity: 1,
                                      doc: <<-EOD) do |args|
  compact_hash
  ============

  Compresses a hash to remove all elements whose values are nil or undef.

  Examples
  --------

  $example = {
    'one'  => 'two',
    'red'  => undef,
    'blue' => nil,
  }

  compact_hash($example)
  # => { 'one => 'two' }

                                      EOD

  hash = args[0]

  hash.reject { |_, val| val.nil? || val == :undef }
end