Class: Packr::Words

Inherits:
Collection show all
Defined in:
lib/packr/words.rb

Defined Under Namespace

Classes: Item

Instance Attribute Summary

Attributes inherited from Collection

#keys, #values

Instance Method Summary collapse

Methods inherited from Collection

#clear, #copy, create, #each, #get_at, #get_keys, #index_of, #initialize, #insert_at, #item, #map, #merge, #put, #put_at, #remove, #remove_at, #reverse!, #size, #slice, #to_s

Methods inherited from Map

#clear, #copy, #each, #get, #get_keys, #get_values, #has?, #initialize, #merge, #put, #remove, #size, #union

Constructor Details

This class inherits a constructor from Packr::Collection

Instance Method Details

#add(word) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/packr/words.rb', line 4

def add(word)
  super unless has?(word)
  word = get(word)
  word.index = size if word.index.zero?
  word.count = word.count + 1
  word
end

#sort!(&sorter) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/packr/words.rb', line 12

def sort!(&sorter)
  return super if block_given?
  super do |word1, word2|
    # sort by frequency
    count = word2.count - word1.count
    index = word1.index - word2.index
    count.nonzero? ? count : (index.nonzero? ? index : 0)
  end
end