Last active
November 8, 2021 20:21
-
-
Save kplawver/d6b277daa37d19b11994282fd30eacb2 to your computer and use it in GitHub Desktop.
This is my personal .irbrc. It's fun.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Object | |
def self.interesting_methods | |
self.public_methods.sort - Object.methods | |
end | |
def interesting_methods | |
self.public_methods.sort - Object.methods | |
end | |
end | |
class Array | |
def average | |
return 0 if self.length < 1 | |
return 0 if self.sum == 0 | |
self.sum.to_f/self.length.to_f | |
end | |
def round(places=0) | |
self.map do |i| | |
i.try(:round, places) || i | |
end | |
end | |
def median(opts={}) | |
return nil if self.length < 1 | |
o = self.dup | |
if opts[:round] | |
if opts[:places].nil? | |
opts[:places] = 0 | |
end | |
o = self.round(opts[:places]) | |
end | |
o.uniq! | |
o.sort! | |
return nil if o.length < 3 | |
i = (o.length / 2).round | |
o[i] | |
end | |
def mode | |
values = {} | |
self.each do |score| | |
score = score.round | |
if values[score.to_s].nil? | |
values[score.to_s] = 1 | |
else | |
values[score.to_s] += 1 | |
end | |
end | |
values = values.sort_by{ |k, v| v }.to_h | |
return 0 if values.keys.last.nil? | |
values.keys.last.to_i | |
end | |
end | |
module Enumerable | |
def mean | |
return self.sum / self.length.to_f | |
end | |
def median | |
return nil if self.length < 1 | |
o = self.sort | |
i = (self.length / 2).floor | |
o[i] | |
end | |
def sample_variance | |
m = self.mean | |
sum = self.inject(0){|accum, i| accum + (i - m) ** 2 } | |
return sum / (self.length - 1).to_f | |
end | |
def standard_deviation | |
return Math.sqrt(self.sample_variance) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment