Module: Enumerable

Defined in:
lib/ruby-util/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#arithmetic_meanObject Also known as: average

Calculates arithmectic mean of all elements in the collection.



30
31
32
# File 'lib/ruby-util/enumerable.rb', line 30

def arithmetic_mean
  size > 0 ? sum.to_f / size : 0
end

#geometric_meanObject

Calculates geometric mean of all elements in the collection.



37
38
39
# File 'lib/ruby-util/enumerable.rb', line 37

def geometric_mean
  size > 0 ? Math.root(product, size) : 0
end

#harmonic_meanObject

Calculates harmonic mean of all elements in the collection.



42
43
44
# File 'lib/ruby-util/enumerable.rb', line 42

def harmonic_mean
  size > 0 ? size / reverse_sum : 0
end

#inversed_sumObject

Calculates inversed sum of the collection.



8
9
10
# File 'lib/ruby-util/enumerable.rb', line 8

def inversed_sum
  1.0 / sum
end

#productObject Also known as: multiply

Multiplicates all elements of the collection.



23
24
25
# File 'lib/ruby-util/enumerable.rb', line 23

def product
  inject(nil) { |mul, x| mul ? mul * x : x }
end

#reverse_sumObject

Calculates reverse sum of the collection.



13
14
15
# File 'lib/ruby-util/enumerable.rb', line 13

def reverse_sum
  sum.to_i > 0 ? inject(nil) { |sum, x| sum ? sum + 1.0 / x : 1.0 / x } : 0
end

#root_mean_squareObject

Calculates root mean square of all elements in the collection.



47
48
49
# File 'lib/ruby-util/enumerable.rb', line 47

def root_mean_square
  size > 0 ? Math.sqrt(square_root_sum / size) : 0
end

#square_root_sumObject

Calculates square root sum of the collection.



18
19
20
# File 'lib/ruby-util/enumerable.rb', line 18

def square_root_sum
  inject(nil) { |sum, x| sum ? sum + x ** 2 : x ** 2 }
end

#sumObject

Calculates sum of the collection.



3
4
5
# File 'lib/ruby-util/enumerable.rb', line 3

def sum
  inject(nil) { |sum, x| sum ? sum + x : x }
end