Sha256: fe2753b908967003a6ed74d58b75bdf513d0a0c77feded32e6ff0758fc6bf5b9

Contents?: true

Size: 1004 Bytes

Versions: 2

Compression:

Stored size: 1004 Bytes

Contents

class Array
  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.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 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

      # If following section will be too small, err on side of too big and lump it in with this one
      if offset + next_size >= size
        next_size = size - offset
      end

      sects << self[offset, offset + next_size]
      offset += next_size
    end

    sects
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bootstripe-0.2.8 lib/bootstripe/array_additions.rb
bootstripe-0.2.7 lib/bootstripe/array_additions.rb