Puppet Function: delete_regex
- Defined in:
- puppet/modules/stdlib/lib/puppet/parser/functions/delete_regex.rb
- Function type:
- Ruby 3.x API
Overview
deletes all instances of a given element that match a regular expression from an array or key from a hash. Multiple regular expressions are assumed to be matched as an OR.
Examples:
delete_regex(['a','b','c','b'], 'b')
Would return: ['a','c']
delete_regex(['a','b','c','b'], ['b', 'c'])
Would return: ['a']
delete_regex({'a'=>1,'b'=>2,'c'=>3}, 'b')
Would return: {'a'=>1,'c'=>3}
delete_regex({'a'=>1,'b'=>2,'c'=>3}, '^a$')
Would return: {'b'=>2,'c'=>3}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'puppet/modules/stdlib/lib/puppet/parser/functions/delete_regex.rb', line 6 newfunction(:delete_regex, :type => :rvalue, :doc => <<-EOS deletes all instances of a given element that match a regular expression from an array or key from a hash. Multiple regular expressions are assumed to be matched as an OR. *Examples:* delete_regex(['a','b','c','b'], 'b') Would return: ['a','c'] delete_regex(['a','b','c','b'], ['b', 'c']) Would return: ['a'] delete_regex({'a'=>1,'b'=>2,'c'=>3}, 'b') Would return: {'a'=>1,'c'=>3} delete_regex({'a'=>1,'b'=>2,'c'=>3}, '^a$') Would return: {'b'=>2,'c'=>3} EOS ) do |arguments| raise(Puppet::ParseError, "delete_regex(): Wrong number of arguments "+ "given #{arguments.size} for 2") unless arguments.size == 2 collection = arguments[0].dup Array(arguments[1]).each do |item| case collection when Array, Hash, String collection.reject! { |coll_item| (coll_item =~ %r{\b#{item}\b}) } else raise(TypeError, "delete_regex(): First argument must be an Array, " + "Hash, or String. Given an argument of class #{collection.class}.") end end collection end |