Class: Dictionary
- Inherits:
-
Object
- Object
- Dictionary
- Defined in:
- lib/ruby-dictionary/word_path.rb,
lib/ruby-dictionary/dictionary.rb
Defined Under Namespace
Classes: WordPath
Class Method Summary collapse
Instance Method Summary collapse
- #==(obj) ⇒ Object
- #exists?(word) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(word_list) ⇒ Dictionary
constructor
A new instance of Dictionary.
- #inspect ⇒ Object
- #starting_with(prefix) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(word_list) ⇒ Dictionary
Returns a new instance of Dictionary.
7 8 9 |
# File 'lib/ruby-dictionary/dictionary.rb', line 7 def initialize(word_list) @word_path = parse_words(word_list) end |
Class Method Details
.from_file(path, separator = "\n") ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ruby-dictionary/dictionary.rb', line 45 def self.from_file(path, separator = "\n") contents = case path when String then File.read(path) when File then path.read else raise ArgumentError, "path must be a String or File" end if contents.start_with?("\x1F\x8B") gz = Zlib::GzipReader.new(StringIO.new(contents)) contents = gz.read end new(contents.split(separator)) end |
Instance Method Details
#==(obj) ⇒ Object
33 34 35 |
# File 'lib/ruby-dictionary/dictionary.rb', line 33 def ==(obj) obj.class == self.class && obj.hash == self.hash end |
#exists?(word) ⇒ Boolean
11 12 13 14 |
# File 'lib/ruby-dictionary/dictionary.rb', line 11 def exists?(word) path = word_path(word) !!(path && path.leaf?) end |
#hash ⇒ Object
29 30 31 |
# File 'lib/ruby-dictionary/dictionary.rb', line 29 def hash self.class.hash ^ @word_path.hash end |
#inspect ⇒ Object
37 38 39 |
# File 'lib/ruby-dictionary/dictionary.rb', line 37 def inspect "#<#{self.class.name}>" end |
#starting_with(prefix) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ruby-dictionary/dictionary.rb', line 16 def starting_with(prefix) prefix = prefix.to_s.strip.downcase path = word_path(prefix) return [] if path.nil? words = [].tap do |words| words << prefix if path.leaf? words.concat(path.suffixes.collect! { |suffix| "#{prefix}#{suffix}" }) end words.sort! end |
#to_s ⇒ Object
41 42 43 |
# File 'lib/ruby-dictionary/dictionary.rb', line 41 def to_s inspect end |