class Analyzer
Attributes
num_array[RW]
Public Class Methods
new(num_array)
click to toggle source
# File lib/analyzer.rb, line 5 def initialize(num_array) self.num_array = num_array end
Public Instance Methods
mean()
click to toggle source
# File lib/analyzer.rb, line 9 def mean self.num_array.inject(&:+)/num_array.count # [a, b, c].inject(&:+) # => ((a + b)+ c)+ d) # [b, c, d].inject(a, &:+) # => ((a + b)+ c)+ d) end
median()
click to toggle source
# File lib/analyzer.rb, line 15 def median sorted = self.num_array.sort! sorted[sorted.count/2] end
mode()
click to toggle source
# File lib/analyzer.rb, line 20 def mode freq = self.num_array.inject(Hash.new(0)) { |h,v| h[v] += 1; h } self.num_array.max_by { |v| freq[v] } end
print_calculations()
click to toggle source
# File lib/analyzer.rb, line 25 def print_calculations puts "Analysis complete, based on #{num_array.length} craigslist listings!" puts "The mean price for your search is #{mean}" puts "The median price for your search is #{median}" puts "The mode price for your search is #{mode}" end