#!/usr/bin/env ruby
class WordDictionary
def initialize(path = '/usr/share/dict/words')
return unless FileTest.file?(path)
@dict = {}
File.open(path, 'r') do |file|
file.each do |line|
if line =~ /^\s*(\w+)\s*$/
@dict[$1] = true
end
end
end
@sortedDictKeys = @dict.keys.sort
end
def findWord(wordFragment="")
return if wordFragment.empty?
puts "dictionary size is #{@dict.size}"
results = []
@sortedDictKeys.each do |key|
results << key if key =~ /#{wordFragment}/
end
results
end
end
dictionary = WordDictionary.new
p dictionary.findWord("shovel")