Skip to content

Instantly share code, notes, and snippets.

@Jared-Prime
Created April 26, 2012 05:04
Show Gist options
  • Save Jared-Prime/2496099 to your computer and use it in GitHub Desktop.
Save Jared-Prime/2496099 to your computer and use it in GitHub Desktop.
class DataSet < Array
def input(data)
if data.kind_of?(Enumerable)
data.each do |item|
if item.kind_of?(Numeric) == true
self << item
else
self.input(item)
end
end
elsif data.kind_of?(Numeric)
self << data
else
begin
raise RuntimeError
rescue
puts "is the data numerable?"
end
end
end
end
## need to extend the Array class in order to perform basic math. Otherwise,
# the methods tend to crash as they attempt to run on Array instances.
class Array
def sum
self.inject(0) do |a,x|
a + x
end
end
def average
self.sum.to_f / self.size
end
def median
case self.size % 2
when 0 then self.sort[self.size/2-1,2].average
when 1 then self.sort[self.size/2].to_f
end if self.size > 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment