Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / delete.rb
1 #
2 # delete.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:delete, :type => :rvalue, :doc => <<-DOC
6     Deletes all instances of a given element from an array, substring from a
7     string, or key from a hash.
8
9     *Examples:*
10
11         delete(['a','b','c','b'], 'b')
12         Would return: ['a','c']
13
14         delete({'a'=>1,'b'=>2,'c'=>3}, 'b')
15         Would return: {'a'=>1,'c'=>3}
16
17         delete({'a'=>1,'b'=>2,'c'=>3}, ['b','c'])
18         Would return: {'a'=>1}
19
20         delete('abracadabra', 'bra')
21         Would return: 'acada'
22
23     Note that from Puppet 4.0.0 the minus (-) operator deletes values from arrays and keys from a hash:
24
25         ['a', 'b', 'c', 'b'] - 'b'
26         # would return ['a', 'c']
27
28         {'a'=>1,'b'=>2,'c'=>3} - ['b','c'])
29         # would return {'a' => '1'}
30
31     A global delete from a string can be performed with the regsubst() function:
32
33         'abracadabra'.regsubst(/bra/, '', 'G')
34         # would return 'acada'
35
36     In general, the filter() function can filter out entries from arrays and hashes based on keys and/or values.
37
38   DOC
39              ) do |arguments|
40
41     raise(Puppet::ParseError, "delete(): Wrong number of arguments given #{arguments.size} for 2") unless arguments.size == 2
42
43     collection = arguments[0].dup
44     Array(arguments[1]).each do |item|
45       case collection
46       when Array, Hash
47         collection.delete item
48       when String
49         collection.gsub! item, ''
50       else
51         raise(TypeError, "delete(): First argument must be an Array, String, or Hash. Given an argument of class #{collection.class}.")
52       end
53     end
54     collection
55   end
56 end
57
58 # vim: set ts=2 sw=2 et :