Class: FreqStats::SuperArray

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/freq_stats/super_array.rb

Overview

creates a more powerful array using rubys simple delgator

Instance Method Summary collapse

Instance Method Details

#freq_listObject

freq_list An array of values and their popularity

Returns:

  • Array of Arrays as [String, Int]

Author:

  • David J. Davis

Since:

  • 0.1.0



30
31
32
33
# File 'lib/freq_stats/super_array.rb', line 30

def freq_list
  freq_hash = each_with_object(Hash.new(0)) { |v, h| h[v] += 1 }
  freq_hash.sort_by { |k, v| -v }
end

#modeObject

mode creates an hash ordered by most frequent element in the array

Author:

  • David J. Davis

Since:

  • 0.1.0



11
12
13
# File 'lib/freq_stats/super_array.rb', line 11

def mode 
  each_with_object(Hash.new(0)) { |v, h| h[v] += 1 }.max_by(&:last)
end

#top_3Object

top_3 sorts by the most frequest items and returns the top 3 most popular ones

Author:

  • David J. Davis

Since:

  • 0.1.0



20
21
22
23
# File 'lib/freq_stats/super_array.rb', line 20

def top_3
  freq_hash = each_with_object(Hash.new(0)) { |v, h| h[v] += 1 }
  freq_hash.sort_by { |k, v| -v }.first(3).map(&:first)
end