Class: Analyzer
- Inherits:
-
Object
- Object
- Analyzer
- Defined in:
- lib/analyzer.rb
Instance Attribute Summary collapse
-
#num_array ⇒ Object
Returns the value of attribute num_array.
Instance Method Summary collapse
-
#initialize(num_array) ⇒ Analyzer
constructor
A new instance of Analyzer.
- #mean ⇒ Object
- #median ⇒ Object
- #mode ⇒ Object
- #print_calculations ⇒ Object
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_array ⇒ Object
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
#mean ⇒ Object
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 |
#median ⇒ Object
15 16 17 18 |
# File 'lib/analyzer.rb', line 15 def median sorted = self.num_array.sort! sorted[sorted.count/2] end |
#mode ⇒ Object
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 |
#print_calculations ⇒ Object
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 |