diff options
author | Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> | 2011-04-29 17:29:14 +0100 |
---|---|---|
committer | Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> | 2011-04-29 17:29:14 +0100 |
commit | b3dd2207c3eefe4a74f86188330a0117b6f03da9 (patch) | |
tree | 596fde2b4e8e5037bb605aa8ddeaa255813805e8 | |
parent | b30ed04390647832002506039457fb93158d3cb8 (diff) |
Small re-factor of shuffle function. It is more compact now.
Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
-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 |