Class: Upwords::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/upwords/dictionary.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(words = []) ⇒ Dictionary

Returns a new instance of Dictionary.



4
5
6
# File 'lib/upwords/dictionary.rb', line 4

def initialize(words = [])
  @legal_words = Set.new(words.map {|w| w.upcase})
end

Class Method Details

.import(filepath) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/upwords/dictionary.rb', line 8

def self.import(filepath)
  dict = Dictionary.new
  File.foreach(filepath) do |line|
    dict << line.chomp
  end
  dict
end

Instance Method Details

#add_word(word) ⇒ Object Also known as: <<



20
21
22
# File 'lib/upwords/dictionary.rb', line 20

def add_word word
  @legal_words.add? (word.upcase)
end

Returns:

  • (Boolean)


16
17
18
# File 'lib/upwords/dictionary.rb', line 16

def legal_word? word
  @legal_words.member? (word.upcase)
end

#remove_word(word) ⇒ Object



26
27
28
# File 'lib/upwords/dictionary.rb', line 26

def remove_word word
  @legal_words.delete? (word.upcase)
end