Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/denmark/monkeypatches.rb

Instance Method Summary collapse

Instance Method Details

#percent_of(digits = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/denmark/monkeypatches.rb', line 4

def percent_of(digits = nil)
  raise "Select the items you want to count using a block that returns a boolean" unless block_given?

  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