Sha256: e17468048c0f2a5624965ac90421a26062218cccfba3b3704c0b88454b9a0780
Contents?: true
Size: 1.06 KB
Versions: 3
Compression:
Stored size: 1.06 KB
Contents
class Array def sum self.inject(:+) end def mean sum / size end def count(&action) return size unless block_given? self.inject(0){ |memo,item| memo += 1 if action.call(item); memo } end def compakt self.reject do |i| i.nil? || (i.respond_to?(:length) && i.length == 0) || (i.respond_to?(:match) && i.match(/^\s+$/)) end end def random(num_elements = 1) num_elements == 1 ? self[Kernel.rand(size)] : sort_by{ Kernel.rand }.slice(0...num_elements) end def random_by_frequency(seed = nil) self.select{|i| i.is_a?(Hash) }.inject({}) do |memo, item| memo[item] = item['frequency'] || item[:frequency] || 0 memo end.random_by_frequency(seed) end def randomized sort_by { rand } end # Return subarrays of random length between a min and max def sections(min_size, max_size) sects = [] offset = 0 while offset < size next_size = (min_size..max_size).random.to_i sects << self[offset..offset+next_size] offset += next_size + 1 end sects end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
bootstripe-0.2.18 | lib/bootstripe/array_additions.rb |
bootstripe-0.2.17 | lib/bootstripe/array_additions.rb |
bootstripe-0.2.16 | lib/bootstripe/array_additions.rb |