Sha256: a28a6b45c2b018f1613f770e186d6ef517eb84dcac4d0fd3fe32629b12472c4f

Contents?: true

Size: 957 Bytes

Versions: 1

Compression:

Stored size: 957 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

  # 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

1 entries across 1 versions & 1 rubygems

Version Path
bootstripe-0.2.6 lib/bootstripe/array_additions.rb