#
# delete_at.rb
#
-
module Puppet::Parser::Functions
- newfunction(:delete_at, :type => :rvalue, :doc => <<-EOS
-Deletes a determined indexed value from an array.
+ newfunction(:delete_at, :type => :rvalue, :doc => <<-DOC
+ Deletes a determined indexed value from an array.
+
+ *Examples:*
+
+ delete_at(['a','b','c'], 1)
+
+ Would return: ['a','c']
+
+ Note that since Puppet 4 this can be done in general with the filter function:
-*Examples:*
+ ['a', 'b', 'c'].filter |$pos, $val | { $pos != 1 }
- delete_at(['a','b','c'], 1)
+ Or if a delete is wanted from the beginning or end of the array, by using the slice operator [ ]:
-Would return: ['a','c']
- EOS
- ) do |arguments|
+ $array[0, -1] # the same as all the values
+ $array[2, -1] # all but the first 2 elements
+ $array[0, -3] # all but the last 2 elements
+ $array[1, -2] # all but the first and last element
+ DOC
+ ) do |arguments|
raise(Puppet::ParseError, "delete_at(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size < 2
index = arguments[1]
- if index.is_a?(String) and not index.match(/^\d+$/)
+ if index.is_a?(String) && !index.match(%r{^\d+$})
raise(Puppet::ParseError, 'delete_at(): You must provide non-negative numeric index')
end