]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
Small re-factor of shuffle function. It is more compact now.
authorKrzysztof Wilczynski <krzysztof.wilczynski@linux.com>
Fri, 29 Apr 2011 16:29:14 +0000 (17:29 +0100)
committerKrzysztof Wilczynski <krzysztof.wilczynski@linux.com>
Fri, 29 Apr 2011 16:29:14 +0000 (17:29 +0100)
Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
shuffle.rb

index c7abf30989f8168e5fb63b153d4e6bc61ff84d31..2bba584af2061c1c923f9ce601c52495c1da93dc 100644 (file)
@@ -18,27 +18,25 @@ module Puppet::Parser::Functions
         'array or string to work with')
     end
 
-    string_given = false
-
     result = value.clone
 
-    if value.is_a?(String)
-      result = result.split('')
-      string_given = true
-    end
-
-    elements = result.size
+    string_type = value.is_a?(String) ? true : false
 
-    return []     if result.size == 0
+    # Check whether it makes sense to shuffle ...
     return result if result.size <= 1
 
+    # We turn any string value into an array to be able to shuffle ...
+    result = string_type ? result.split('') : result
+
+    elements = result.size
+
     # Simple implementation of Fisher–Yates in-place shuffle ...
     elements.times do |i|
       j = rand(elements - i) + i
       result[j], result[i] = result[i], result[j]
     end
 
-    result = string_given ? result.join : result
+    result = string_type ? result.join : result
 
     return result
   end