diff options
author | James Turnbull <james@lovedthanlost.net> | 2009-06-12 22:39:33 +1000 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2009-06-12 22:39:33 +1000 |
commit | 5fbf63ce789b015da9abb95d7e9fbbf4a44ba7d1 (patch) | |
tree | 3dbf7e79fe773530a2ad007246b3e30d5fd925f0 /lib/puppet/parser/functions/split.rb | |
parent | a585bddadf49b1c1d41052f6fdd62db832bca49c (diff) | |
download | puppet-5fbf63ce789b015da9abb95d7e9fbbf4a44ba7d1.tar.gz puppet-5fbf63ce789b015da9abb95d7e9fbbf4a44ba7d1.tar.xz puppet-5fbf63ce789b015da9abb95d7e9fbbf4a44ba7d1.zip |
Updated split function and add split function unit tests (courtesy of Thomas Bellman)
Diffstat (limited to 'lib/puppet/parser/functions/split.rb')
-rw-r--r-- | lib/puppet/parser/functions/split.rb | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/lib/puppet/parser/functions/split.rb b/lib/puppet/parser/functions/split.rb index 8932bfe38..36dba7e68 100644 --- a/lib/puppet/parser/functions/split.rb +++ b/lib/puppet/parser/functions/split.rb @@ -1,13 +1,29 @@ module Puppet::Parser::Functions newfunction(:split, :type => :rvalue, - :doc => "Split a string variable into an array using the specified split character. + :doc => "\ +Split a string variable into an array using the specified split regexp. -Usage:: + Usage:: - $string = 'value1,value2' - $array_var = split($string, ',') + $string = 'v1.v2:v3.v4' + $array_var1 = split($string, ':') + $array_var2 = split($string, '[.]') + $array_var3 = split($string, '[.:]') -$array_var holds the result ['value1', 'value2']") do |args| - return args[0].split(args[1]) +$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 string that contains +a regexp meta-character (.), and that needs protection. A simple +way to do that for a single character is to enclose it in square +brackets.") do |args| + + if args.length != 2 + raise Puppet::ParseError, ("split(): wrong number of arguments" + + " (#{args.length}; must be 2)") + end + + return args[0].split(Regexp.compile(args[1])) end end |