Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/overloads/array.rb
Instance Method Summary collapse
-
#merge_hashes_using_key(key) ⇒ Object
For reference: stackoverflow.com/questions/27249327/how-to-merge-hashes-if-a-specified-keys-values-are-same-in-a-array Credits to stackoverflow.com/users/390819/w0lf for the solution.
-
#stdevp ⇒ Object
For reference: www.mathsisfun.com/data/standard-deviation-formulas.html Population standard deviation.
Instance Method Details
#merge_hashes_using_key(key) ⇒ Object
For reference: stackoverflow.com/questions/27249327/how-to-merge-hashes-if-a-specified-keys-values-are-same-in-a-array Credits to stackoverflow.com/users/390819/w0lf for the solution
26 27 28 29 30 31 32 |
# File 'lib/overloads/array.rb', line 26 def merge_hashes_using_key(key) return false unless self.length > 0 arr = self arr.group_by{ |h| h[key] }.collect{ |_, hs| hs.reduce(:merge) } end |
#stdevp ⇒ Object
For reference: www.mathsisfun.com/data/standard-deviation-formulas.html Population standard deviation
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/overloads/array.rb', line 5 def stdevp return false unless self.length > 0 arr = self # Step 1: Get the mean mean = arr.inject(:+).to_f / arr.length.to_f # Step 2: Subtract the mean and square the result step_2 = arr.reduce([]) do |i, n| i << (n.to_f - mean) ** 2 end # Step 3: Get mean of step 2 step_3 = step_2.inject(:+).to_f / step_2.length.to_f # Step 4: Square root of step 3 Math.sqrt(step_3).to_f end |