Class: Upwords::Dictionary
- Inherits:
-
Object
- Object
- Upwords::Dictionary
- Defined in:
- lib/upwords/dictionary.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_word(word) ⇒ Object (also: #<<)
-
#initialize(words = []) ⇒ Dictionary
constructor
A new instance of Dictionary.
- #legal_word?(word) ⇒ Boolean
- #remove_word(word) ⇒ Object
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 |
#legal_word?(word) ⇒ 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 |