Class: FreqStats::SuperArray
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- FreqStats::SuperArray
- Defined in:
- lib/freq_stats/super_array.rb
Overview
creates a more powerful array using rubys simple delgator
Instance Method Summary collapse
-
#freq_list ⇒ Object
freq_list An array of values and their popularity.
-
#mode ⇒ Object
mode creates an hash ordered by most frequent element in the array.
-
#top_3 ⇒ Object
top_3 sorts by the most frequest items and returns the top 3 most popular ones.
Instance Method Details
#freq_list ⇒ Object
freq_list An array of values and their popularity
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 |
#mode ⇒ Object
mode creates an hash ordered by most frequent element in the array
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_3 ⇒ Object
top_3 sorts by the most frequest items and returns the top 3 most popular ones
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 |