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