Sha256: 154b36f43d348324c64094fe0a0c5b25b949d77cd08c23fe1b0d5beacea4a0bd

Contents?: true

Size: 1.26 KB

Versions: 202

Compression:

Stored size: 1.26 KB

Contents

# Split a string variable into an array using the specified pattern.
# The pattern can be a string, regexp or regexp type.
#
# @example
#
#     $string     = 'v1.v2:v3.v4'
#     $array_var1 = split($string, /:/)
#     $array_var2 = split($string, '[.]')
#     $array_var3 = split($string, Regexp['[.:]'])
#
# `$array_var1` now holds the result `['v1.v2', 'v3.v4']`,
# while `$array_var2` holds `['v1', 'v2:v3', 'v4']`, and
# `$array_var3` holds `['v1', 'v2', 'v3', 'v4']`.
#
# Note that in the second example, we split on a literal string that contains
# a regexp meta-character (.), which must be escaped.  A simple
# way to do that for a single character is to enclose it in square
# brackets; a backslash will also escape a single character.
#
Puppet::Functions.create_function(:split) do
  dispatch :split_String do
    param 'String', :str
    param 'String', :pattern
  end

  dispatch :split_Regexp do
    param 'String', :str
    param 'Regexp', :pattern
  end

  dispatch :split_RegexpType do
    param 'String', :str
    param 'Type[Regexp]', :pattern
  end

  def split_String(str, pattern)
    str.split(Regexp.compile(pattern))
  end

  def split_Regexp(str, pattern)
    str.split(pattern)
  end

  def split_RegexpType(str, pattern)
    str.split(pattern.regexp)
  end
end

Version data entries

202 entries across 202 versions & 3 rubygems

Version Path
puppet-retrospec-1.8.0 vendor/pup410/lib/puppet/functions/split.rb
puppet-retrospec-1.7.0 vendor/pup410/lib/puppet/functions/split.rb
puppet-5.3.7 lib/puppet/functions/split.rb
puppet-5.3.7-x86-mingw32 lib/puppet/functions/split.rb
puppet-5.3.7-x64-mingw32 lib/puppet/functions/split.rb
puppet-5.3.7-universal-darwin lib/puppet/functions/split.rb
puppet-4.10.12 lib/puppet/functions/split.rb
puppet-4.10.12-x86-mingw32 lib/puppet/functions/split.rb
puppet-4.10.12-x64-mingw32 lib/puppet/functions/split.rb
puppet-4.10.12-universal-darwin lib/puppet/functions/split.rb
puppet-4.10.11 lib/puppet/functions/split.rb
puppet-4.10.11-x86-mingw32 lib/puppet/functions/split.rb
puppet-4.10.11-x64-mingw32 lib/puppet/functions/split.rb
puppet-4.10.11-universal-darwin lib/puppet/functions/split.rb
puppet-5.3.6 lib/puppet/functions/split.rb
puppet-5.3.6-x86-mingw32 lib/puppet/functions/split.rb
puppet-5.3.6-x64-mingw32 lib/puppet/functions/split.rb
puppet-5.3.6-universal-darwin lib/puppet/functions/split.rb
bolt-0.17.1 vendored/puppet/lib/puppet/functions/split.rb
bolt-0.17.0 vendored/puppet/lib/puppet/functions/split.rb