Module: Enumerable
- Defined in:
- lib/ruby-util/enumerable.rb
Instance Method Summary collapse
-
#arithmetic_mean ⇒ Object
(also: #average)
Calculates arithmectic mean of all elements in the collection.
-
#geometric_mean ⇒ Object
Calculates geometric mean of all elements in the collection.
-
#harmonic_mean ⇒ Object
Calculates harmonic mean of all elements in the collection.
-
#inversed_sum ⇒ Object
Calculates inversed sum of the collection.
-
#product ⇒ Object
(also: #multiply)
Multiplicates all elements of the collection.
-
#reverse_sum ⇒ Object
Calculates reverse sum of the collection.
-
#root_mean_square ⇒ Object
Calculates root mean square of all elements in the collection.
-
#square_root_sum ⇒ Object
Calculates square root sum of the collection.
-
#sum ⇒ Object
Calculates sum of the collection.
Instance Method Details
#arithmetic_mean ⇒ Object 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_mean ⇒ Object
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_mean ⇒ Object
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_sum ⇒ Object
Calculates inversed sum of the collection.
8 9 10 |
# File 'lib/ruby-util/enumerable.rb', line 8 def inversed_sum 1.0 / sum end |
#product ⇒ Object 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_sum ⇒ Object
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_square ⇒ Object
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_sum ⇒ Object
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 |
#sum ⇒ Object
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 |