class Anagram::Finder

Public Class Methods

from_file(file_name) click to toggle source
# File lib/anagram/finder.rb, line 3
def self.from_file(file_name)
        new(File.readlines(file_name))
end
new(dictionary_words) click to toggle source
# File lib/anagram/finder.rb, line 7
def initialize(dictionary_words)
        @signatures = {}
        dictionary_words.each do |line|
                word = line.chomp
                signature = Finder.signature_of(word)
                (@signatures[signature] ||= []) << word
        end
end
signature_of(word) click to toggle source
# File lib/anagram/finder.rb, line 21
def self.signature_of(word)
        word.unpack("c*").sort.pack("c*")
end

Public Instance Methods

lookup(word) click to toggle source
# File lib/anagram/finder.rb, line 16
def lookup(word)
        signature = Finder.signature_of(word)
        @signatures[signature]
end