Class: Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/analyzer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num_array) ⇒ Analyzer



5
6
7
# File 'lib/analyzer.rb', line 5

def initialize(num_array)
  self.num_array = num_array
end

Instance Attribute Details

#num_arrayObject

Returns the value of attribute num_array.



3
4
5
# File 'lib/analyzer.rb', line 3

def num_array
  @num_array
end

Instance Method Details

#meanObject



9
10
11
12
13
# 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

#medianObject



15
16
17
18
# File 'lib/analyzer.rb', line 15

def median
  sorted = self.num_array.sort!
  sorted[sorted.count/2]
end

#modeObject



20
21
22
23
# 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


25
26
27
28
29
30
# 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