Class: Aliasing::History
- Inherits:
-
Object
- Object
- Aliasing::History
- Defined in:
- lib/aliasing/history.rb
Instance Attribute Summary collapse
-
#by_frequency ⇒ Object
Returns the value of attribute by_frequency.
-
#entries ⇒ Object
Returns the value of attribute entries.
Instance Method Summary collapse
-
#initialize(entries) ⇒ History
constructor
A new instance of History.
- #most_frequent(num) ⇒ Object
- #sort_by_frequency ⇒ Object
Constructor Details
#initialize(entries) ⇒ History
Returns a new instance of History.
6 7 8 9 10 |
# File 'lib/aliasing/history.rb', line 6 def initialize(entries) @entries = entries @by_frequency = {} sort_by_frequency end |
Instance Attribute Details
#by_frequency ⇒ Object
Returns the value of attribute by_frequency.
4 5 6 |
# File 'lib/aliasing/history.rb', line 4 def by_frequency @by_frequency end |
#entries ⇒ Object
Returns the value of attribute entries.
4 5 6 |
# File 'lib/aliasing/history.rb', line 4 def entries @entries end |
Instance Method Details
#most_frequent(num) ⇒ Object
23 24 25 |
# File 'lib/aliasing/history.rb', line 23 def most_frequent(num) Hash[@by_frequency[0..num-1]] end |
#sort_by_frequency ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/aliasing/history.rb', line 12 def sort_by_frequency entries_with_freqency = {} @entries.each { |e| entries_with_freqency[e] ||= 0 entries_with_freqency[e] += 1 } @by_frequency = entries_with_freqency.sort_by{|k,v| v}.reverse end |