Skip to content

Instantly share code, notes, and snippets.

@Nephos
Created July 2, 2017 12:58
Show Gist options
  • Save Nephos/8807fe0940f22edf6585ae6fb6dcf5cd to your computer and use it in GitHub Desktop.
Save Nephos/8807fe0940f22edf6585ae6fb6dcf5cd to your computer and use it in GitHub Desktop.
class Array
def average
self.sum / self.size.to_f
end
def sum
self.reduce(0) { |l, r| l + r }
end
# TODO: check
def std_deviation
center = self.average
self.reduce(0) { |base, v| base += v - center }
end
def chartifize
values = self.map(&:attributes)
chart = {}
values.first&.keys&.each { |k| chart[k] = [] }
values.each { |line| line.each { |k, v| chart[k] << v } }
chart
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment