diff options
Diffstat (limited to 'shuffle.rb')
-rw-r--r-- | shuffle.rb | 18 |
1 files changed, 8 insertions, 10 deletions
@@ -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 |