4 module Puppet::Parser::Functions
5 newfunction(:any2array, :type => :rvalue, :doc => <<-DOC
6 This converts any object to an array containing that object. Empty argument
7 lists are converted to an empty array. Arrays are left untouched. Hashes are
8 converted to arrays of alternating keys and values.
10 Note that since Puppet 5.0.0 it is possible to create new data types for almost any
11 datatype using the type system and the built-in
12 [`Array.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-array-and-tuple)
13 function is used to create a new Array..
15 $hsh = {'key' => 42, 'another-key' => 100}
18 Would notice `[['key', 42], ['another-key', 100]]`
20 The Array data type also has a special mode to "create an array if not already an array"
22 notice(Array({'key' => 42, 'another-key' => 100}, true))
24 Would notice `[{'key' => 42, 'another-key' => 100}]`, as the `true` flag prevents the hash from being
25 transformed into an array.
33 return arguments unless arguments.length == 1
34 return arguments[0] if arguments[0].is_a?(Array)
35 return [] if arguments == ['']
36 if arguments[0].is_a?(Hash)
38 arguments[0].each do |key, value|
39 result << key << value
47 # vim: set ts=2 sw=2 et :