Puppet Function: hash_select_re
- Defined in:
- puppet/modules/wmflib/lib/puppet/parser/functions/hash_select_re.rb
- Function type:
- Ruby 3.x API
Overview
This function creates a new hash from the input hash, filtering out keys which do not match the provided regex.
Examples:
$in = { 'abc' => 1, 'def' => 2, 'asdf' => 3 }
$out = hash_select_re('^a', $in);
# $out == { 'abc' => 1, 'asdf' => 3 }
$out2 = hash_select_re('^(?!a)', $in);
# $out2 == { 'def' => 2 }
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 |
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/hash_select_re.rb', line 6 newfunction(:hash_select_re, :type => :rvalue, :doc => <<-EOS This function creates a new hash from the input hash, filtering out keys which do not match the provided regex. *Examples:* $in = { 'abc' => 1, 'def' => 2, 'asdf' => 3 } $out = hash_select_re('^a', $in); # $out == { 'abc' => 1, 'asdf' => 3 } $out2 = hash_select_re('^(?!a)', $in); # $out2 == { 'def' => 2 } EOS ) do |arguments| unless arguments.size == 2 raise(Puppet::ParseError, "hash_select_re(): Wrong number of arguments " + "given (#{arguments.size} for 2)") end pattern = Regexp.new(arguments[0]) in_hash = arguments[1] unless in_hash.is_a?(Hash) raise(Puppet::ParseError, 'hash_select_re(): Argument 2 must be a hash') end # https://bibwild.wordpress.com/2012/04/12/ruby-hash-select-1-8-7-and-1-9-3-simultaneously-compatible/ Hash[ in_hash.select { |k, _v| pattern.match(k) } ] end |