Class: Dictionary
- Inherits:
-
Object
- Object
- Dictionary
- Defined in:
- lib/ruby-dictionary/version.rb,
lib/ruby-dictionary/word_path.rb,
lib/ruby-dictionary/dictionary.rb
Defined Under Namespace
Classes: WordPath
Constant Summary collapse
- VERSION =
'1.1.1'
Class Method Summary collapse
Instance Method Summary collapse
- #==(obj) ⇒ Object
- #case_sensitive? ⇒ Boolean
- #exists?(word) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(word_list, case_sensitive = false) ⇒ Dictionary
constructor
A new instance of Dictionary.
- #inspect ⇒ Object
- #prefixes(string) ⇒ Object
- #starting_with(prefix) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(word_list, case_sensitive = false) ⇒ Dictionary
Returns a new instance of Dictionary.
7 8 9 |
# File 'lib/ruby-dictionary/dictionary.rb', line 7 def initialize(word_list, case_sensitive = false) @word_path = parse_words(word_list, case_sensitive) end |
Class Method Details
.from_file(path, separator = "\n", case_sensitive = false) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ruby-dictionary/dictionary.rb', line 59 def self.from_file(path, separator = "\n", case_sensitive = false) 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), case_sensitive) end |
Instance Method Details
#==(obj) ⇒ Object
47 48 49 |
# File 'lib/ruby-dictionary/dictionary.rb', line 47 def ==(obj) obj.class == self.class && obj.hash == self.hash end |
#case_sensitive? ⇒ Boolean
11 12 13 |
# File 'lib/ruby-dictionary/dictionary.rb', line 11 def case_sensitive? @word_path.case_sensitive? end |
#exists?(word) ⇒ Boolean
15 16 17 18 |
# File 'lib/ruby-dictionary/dictionary.rb', line 15 def exists?(word) path = word_path(word) !!(path && path.leaf?) end |
#hash ⇒ Object
43 44 45 |
# File 'lib/ruby-dictionary/dictionary.rb', line 43 def hash self.class.hash ^ @word_path.hash end |
#inspect ⇒ Object
51 52 53 |
# File 'lib/ruby-dictionary/dictionary.rb', line 51 def inspect "#<#{self.class.name}>" end |
#prefixes(string) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/ruby-dictionary/dictionary.rb', line 35 def prefixes(string) string = string.to_s.strip string = string.downcase unless case_sensitive? @word_path.find_prefixes(string).sort end |
#starting_with(prefix) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ruby-dictionary/dictionary.rb', line 20 def starting_with(prefix) prefix = prefix.to_s.strip prefix = prefix.downcase unless case_sensitive? 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
55 56 57 |
# File 'lib/ruby-dictionary/dictionary.rb', line 55 def to_s inspect end |