Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/fnordmetric/ext.rb

Instance Method Summary collapse

Instance Method Details

#emtpyObject



60
61
62
# File 'lib/fnordmetric/ext.rb', line 60

def emtpy
  self.size == 0
end

#meanObject Also known as: average



37
38
39
40
# File 'lib/fnordmetric/ext.rb', line 37

def mean
  return 0 if empty?
  inject(&:+).to_f / size
end

#medianObject



44
45
46
47
# File 'lib/fnordmetric/ext.rb', line 44

def median
  return 0 if empty?
  (_sorted = self.dup.sort)[_sorted.size/2]
end

#modeObject



54
55
56
57
58
# File 'lib/fnordmetric/ext.rb', line 54

def mode
  return 0 if empty?
  inject({}){ |h,v| h[v] = h[v].to_i+1; h }.to_a
  .sort{ |a,b| b.last <=> a.last }[0][0]
end

#rangeObject



49
50
51
52
# File 'lib/fnordmetric/ext.rb', line 49

def range
  return 0 if empty?
  max - min
end