class FreqStats::SuperArray
creates a more powerful array using rubys simple delgator
Public Instance Methods
freq_list()
click to toggle source
freq_list
An array of values and their popularity @author David J. Davis @since 0.1.0 @return Array of Arrays as [String, Int]
# 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
mode()
click to toggle source
mode creates an hash ordered by most frequent element in the array @author David J. Davis @since 0.1.0 @return Array [String, Int]
# 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_3()
click to toggle source
top_3
sorts by the most frequest items and returns the top 3 most popular ones @author David J. Davis @since 0.1.0 @return Array [String, String, String]
# 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