class Array

Public Instance Methods

percent_of(digits = nil) { |elem| ... } click to toggle source
# File lib/denmark/monkeypatches.rb, line 6
def percent_of(digits = nil)
  raise "Select the items you want to count using a block that returns a boolean" unless block_given?
  return 0 if self.empty?

  count = self.size
  match = 0
  self.each do |elem|
    match += 1 if yield(elem)
  end

  if digits
    ((match.to_f / count.to_f) * 100).round(digits)
  else
    ((match.to_f / count.to_f) * 100).to_i
  end
end