Module: Faker::ArrayUtils
- Defined in:
- lib/ffakerer/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/ffakerer/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
18 19 20 21 22 |
# File 'lib/ffakerer/utils/array_utils.rb', line 18 def self.freeze_all(array) array.each { |e| e.freeze } array.freeze array end |
.rand(array) ⇒ Object
14 15 16 |
# File 'lib/ffakerer/utils/array_utils.rb', line 14 def self.rand(array) array[Kernel.rand(array.length)].dup end |
.random_pick(array, n) ⇒ Object
9 10 11 12 |
# File 'lib/ffakerer/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
24 25 26 |
# File 'lib/ffakerer/utils/array_utils.rb', line 24 def self.shuffle(array) array.sort_by{Kernel.rand} end |
Instance Method Details
#freeze_all ⇒ Object
36 37 38 |
# File 'lib/ffakerer/utils/array_utils.rb', line 36 def freeze_all ArrayUtils.freeze_all(self) end |
#rand ⇒ Object
32 33 34 |
# File 'lib/ffakerer/utils/array_utils.rb', line 32 def rand ArrayUtils.rand(self) end |
#random_pick(n) ⇒ Object
28 29 30 |
# File 'lib/ffakerer/utils/array_utils.rb', line 28 def random_pick(n) ArrayUtils.random_pick(self, n) end |
#shuffle ⇒ Object
40 41 42 |
# File 'lib/ffakerer/utils/array_utils.rb', line 40 def shuffle ArrayUtils.shuffle(self) end |