X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=3rdparty%2Fmodules%2Fstdlib%2Flib%2Fpuppet%2Fparser%2Ffunctions%2Fdelete_regex.rb;h=8093896f4c149987fa14ceae007c051755877116;hb=131e09855e065be940e104d9ab0f18940cc76257;hp=e5dc1fdcd3e8a5728f4cc348cab9cdf10d3cf082;hpb=407d322498f4fde815abf381007fbecfe5c10b2b;p=mirror%2Fdsa-puppet.git diff --git a/3rdparty/modules/stdlib/lib/puppet/parser/functions/delete_regex.rb b/3rdparty/modules/stdlib/lib/puppet/parser/functions/delete_regex.rb index e5dc1fdcd..8093896f4 100644 --- a/3rdparty/modules/stdlib/lib/puppet/parser/functions/delete_regex.rb +++ b/3rdparty/modules/stdlib/lib/puppet/parser/functions/delete_regex.rb @@ -2,39 +2,44 @@ # delete_regex.rb # Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. # - module Puppet::Parser::Functions - 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. + newfunction(:delete_regex, :type => :rvalue, :doc => <<-DOC + 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'] -*Examples:* + delete_regex({'a'=>1,'b'=>2,'c'=>3}, 'b') + Would return: {'a'=>1,'c'=>3} - delete_regex(['a','b','c','b'], 'b') - Would return: ['a','c'] + delete_regex({'a'=>1,'b'=>2,'c'=>3}, '^a$') + Would return: {'b'=>2,'c'=>3} - delete_regex(['a','b','c','b'], ['b', 'c']) - Would return: ['a'] + Note that since Puppet 4 this can be done in general with the filter function: - delete_regex({'a'=>1,'b'=>2,'c'=>3}, 'b') - Would return: {'a'=>1,'c'=>3} + ["aaa", "aba", "aca"].filter |$val| { $val !~ /b/ } + # Would return: ['aaa', 'aca'] - delete_regex({'a'=>1,'b'=>2,'c'=>3}, '^a$') - Would return: {'b'=>2,'c'=>3} - EOS - ) do |arguments| + DOC + ) 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}.") + 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