Module: Faker::ArrayUtils
- Defined in:
- lib/ffaker/utils/array_utils.rb
Class Method Summary collapse
- .const_array(argument) ⇒ Object
- .freeze_all(array) ⇒ Object
- .rand(array) ⇒ Object
- .random_pick(array, n) ⇒ Object
- .shuffle(array) ⇒ Object
Instance Method Summary collapse
Class Method Details
.const_array(argument) ⇒ Object
3 4 5 6 7 |
# File 'lib/ffaker/utils/array_utils.rb', line 3 def self.const_array(argument) array = argument.is_a?(Array) ? argument : argument.to_a array.extend ArrayUtils freeze_all(array) end |
.freeze_all(array) ⇒ Object
19 20 21 22 23 |
# File 'lib/ffaker/utils/array_utils.rb', line 19 def self.freeze_all(array) array.each(&:freeze) array.freeze array end |
.rand(array) ⇒ Object
14 15 16 17 |
# File 'lib/ffaker/utils/array_utils.rb', line 14 def self.rand(array) warn '[ArrayUtils.rand] is deprecated. Please use the Array#sample method' array.sample end |
.random_pick(array, n) ⇒ Object
9 10 11 12 |
# File 'lib/ffaker/utils/array_utils.rb', line 9 def self.random_pick(array, n) indexes = (0...array.length).sort_by{Kernel.rand}[0...n] indexes.map { |n| array[n].dup } end |
.shuffle(array) ⇒ Object
25 26 27 |
# File 'lib/ffaker/utils/array_utils.rb', line 25 def self.shuffle(array) array.sort_by{Kernel.rand} end |
Instance Method Details
#freeze_all ⇒ Object
38 39 40 |
# File 'lib/ffaker/utils/array_utils.rb', line 38 def freeze_all ArrayUtils.freeze_all(self) end |
#rand ⇒ Object
33 34 35 36 |
# File 'lib/ffaker/utils/array_utils.rb', line 33 def rand warn '[ArrayUtils#rand] is deprecated. Please use the Array#sample method' sample end |
#random_pick(n) ⇒ Object
29 30 31 |
# File 'lib/ffaker/utils/array_utils.rb', line 29 def random_pick(n) ArrayUtils.random_pick(self, n) end |
#shuffle ⇒ Object
42 43 44 |
# File 'lib/ffaker/utils/array_utils.rb', line 42 def shuffle ArrayUtils.shuffle(self) end |