summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrzysztof Wilczynski <krzysztof.wilczynski@linux.com>2011-04-29 05:00:51 +0100
committerKrzysztof Wilczynski <krzysztof.wilczynski@linux.com>2011-04-29 05:00:51 +0100
commite9d0b65807532903ff5ce8bd8f05149089044264 (patch)
tree331ded750b0d977a69dcfeb5f7cec20973806f3f
parent1296b3fc28570ab553d765ba93ba130c6a1a0dcb (diff)
downloadpuppet-stdlib-e9d0b65807532903ff5ce8bd8f05149089044264.tar.gz
puppet-stdlib-e9d0b65807532903ff5ce8bd8f05149089044264.tar.bz2
Adding support for string alongside arrays.
Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
-rw-r--r--shuffle.rb22
1 files changed, 16 insertions, 6 deletions
diff --git a/shuffle.rb b/shuffle.rb
index 33c32bb..c7abf30 100644
--- a/shuffle.rb
+++ b/shuffle.rb
@@ -2,8 +2,6 @@
# shuffle.rb
#
-# TODO(Krzysztof Wilczynski): Support for strings would be nice too ...
-
module Puppet::Parser::Functions
newfunction(:shuffle, :type => :rvalue, :doc => <<-EOS
EOS
@@ -12,13 +10,23 @@ module Puppet::Parser::Functions
raise(Puppet::ParseError, "shuffle(): Wrong number of arguments " +
"given (#{arguments.size} for 1)") if arguments.size < 1
- array = arguments[0]
+ value = arguments[0]
+ klass = value.class
+
+ if not [Array, String].include?(klass)
+ raise(Puppet::ParseError, 'shuffle(): Requires either an ' +
+ 'array or string to work with')
+ end
+
+ string_given = false
+
+ result = value.clone
- if not array.is_a?(Array)
- raise(Puppet::ParseError, 'shuffle(): Requires an array to work with')
+ if value.is_a?(String)
+ result = result.split('')
+ string_given = true
end
- result = array.clone
elements = result.size
return [] if result.size == 0
@@ -30,6 +38,8 @@ module Puppet::Parser::Functions
result[j], result[i] = result[i], result[j]
end
+ result = string_given ? result.join : result
+
return result
end
end