module Riveter::CoreExtensions::ArrayExtensions

Public Instance Methods

average() click to toggle source

average of a set of numbers

# File lib/riveter/core_extensions.rb, line 57
def average
  self.sum / self.length.to_f
end
cumulative_sum() click to toggle source
# File lib/riveter/core_extensions.rb, line 38
def cumulative_sum
  sum = 0
  self.map {|x| sum += x}
end
find_each(&block)
find_each_with_order(&block) click to toggle source

make compatible with an ActiveRelation

# File lib/riveter/core_extensions.rb, line 93
def find_each_with_order(&block)
  self.each(&block) if block_given?
end
Also aliased as: find_each
nil_sum(identity = nil) { |x| ... } click to toggle source

pure genious

# File lib/riveter/core_extensions.rb, line 44
def nil_sum(identity = nil, &block)
  if block_given?
    self.inject(identity) {|sum, x|
      ((sum || 0) + (yield(x) || 0)) || sum
    }
  else
    self.inject(identity) {|sum, x|
      ((sum || 0) + (x || 0)) || sum
    }
  end
end
round(places) click to toggle source

rounds each items to the specified number of places

# File lib/riveter/core_extensions.rb, line 88
def round(places)
  self.collect {|x| x.round(places) }
end
standard_deviation() click to toggle source

standard deviation of an array of numbers

# File lib/riveter/core_extensions.rb, line 68
def standard_deviation
  Math.sqrt(self.variance)
end
to_hash_for() { |record| ... } click to toggle source

returns a hash of the items, indexed by the given items attribute

# File lib/riveter/core_extensions.rb, line 73
def to_hash_for(&block)
  if block_given?
    self.inject({}) do |hash, record|
      hash[yield(record)] = record
      hash
    end
  else
    self.inject({}) do |hash, record|
      hash[record] = record
      hash
    end
  end
end
variance() click to toggle source

variance of a set of numbers

# File lib/riveter/core_extensions.rb, line 62
def variance
  avg = self.average
  self.inject(0.0) {|acc, value| acc + ((value - avg)**2) } / (self.length.to_f - 1)
end