Class: Aliasing::History

Inherits:
Object
  • Object
show all
Defined in:
lib/aliasing/history.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_frequencyObject

Returns the value of attribute by_frequency.



4
5
6
# File 'lib/aliasing/history.rb', line 4

def by_frequency
  @by_frequency
end

#entriesObject

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_frequencyObject



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