Puppet Function: stdlib::nested_values

Defined in:
lib/puppet/functions/stdlib/nested_values.rb
Function type:
Ruby 4.x API

Summary

Get list of nested values from given hash This function will return list of nested Hash values and returns list of values in form of Array

Overview

stdlib::nested_values(Hash $hash)Array

Examples:

Example Usage:

$hash = {
  "key1" => "value1",
  "key2" => { "key2.1" => "value2.1"},
  "key3" => "value3"
}
$data = $hash.stdlib::nested_values
#Output : ["value1", "value2.1", "value3"]

Parameters:

  • hash (Hash)

    A (nested) hash

Returns:

  • (Array)

    All the values found in the input hash included those deeply nested.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/puppet/functions/stdlib/nested_values.rb', line 14

Puppet::Functions.create_function(:'stdlib::nested_values') do
  # @param hash A (nested) hash
  # @return All the values found in the input hash included those deeply nested.
  dispatch :nested_values do
    param 'Hash', :hash
    return_type 'Array'
  end

  def nested_values(hash)
    hash.each_with_object([]) do |(_k, v), values|
      v.is_a?(Hash) ? values.concat(nested_values(v)) : (values << v)
    end
  end
end