Mercurial > repos > other > Puppet
comparison modules/stdlib/lib/puppet/parser/functions/shuffle.rb @ 478:adf6fe9bbc17
Update Puppet modules to latest versions
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Thu, 29 Aug 2024 18:47:29 +0100 |
parents | d9352a684e62 |
children |
comparison
equal
deleted
inserted
replaced
477:21f6add30502 | 478:adf6fe9bbc17 |
---|---|
1 # frozen_string_literal: true | |
2 | |
1 # | 3 # |
2 # shuffle.rb | 4 # shuffle.rb |
3 # | 5 # |
4 module Puppet::Parser::Functions | 6 module Puppet::Parser::Functions |
5 newfunction(:shuffle, :type => :rvalue, :doc => <<-DOC | 7 newfunction(:shuffle, type: :rvalue, doc: <<-DOC |
6 @summary | 8 @summary |
7 Randomizes the order of a string or array elements. | 9 Randomizes the order of a string or array elements. |
8 | 10 |
9 @return | 11 @return |
10 randomized string or array | 12 randomized string or array |
11 DOC | 13 DOC |
12 ) do |arguments| | 14 ) do |arguments| |
13 | |
14 raise(Puppet::ParseError, "shuffle(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? | 15 raise(Puppet::ParseError, "shuffle(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? |
15 | 16 |
16 value = arguments[0] | 17 value = arguments[0] |
17 | 18 |
18 unless value.is_a?(Array) || value.is_a?(String) | 19 raise(Puppet::ParseError, 'shuffle(): Requires either array or string to work with') unless value.is_a?(Array) || value.is_a?(String) |
19 raise(Puppet::ParseError, 'shuffle(): Requires either array or string to work with') | |
20 end | |
21 | 20 |
22 result = value.clone | 21 result = value.clone |
23 | 22 |
24 string = value.is_a?(String) ? true : false | 23 string = value.is_a?(String) |
25 | 24 |
26 # Check whether it makes sense to shuffle ... | 25 # Check whether it makes sense to shuffle ... |
27 return result if result.size <= 1 | 26 return result if result.size <= 1 |
28 | 27 |
29 # We turn any string value into an array to be able to shuffle ... | 28 # We turn any string value into an array to be able to shuffle ... |
30 result = string ? result.split('') : result | 29 result = string ? result.chars : result |
31 | 30 |
32 elements = result.size | 31 elements = result.size |
33 | 32 |
34 # Simple implementation of Fisher–Yates in-place shuffle ... | 33 # Simple implementation of Fisher–Yates in-place shuffle ... |
35 elements.times do |i| | 34 elements.times do |i| |