Class: Omelettes::Words
- Inherits:
-
Object
- Object
- Omelettes::Words
- Defined in:
- lib/omelettes/words.rb
Class Method Summary collapse
- .add(word) ⇒ Object
- .clear ⇒ Object
- .load(path = "/usr/share/dict/words") ⇒ Object
- .replace(word) ⇒ Object
- .word_hash ⇒ Object
Class Method Details
.add(word) ⇒ Object
9 10 11 12 13 |
# File 'lib/omelettes/words.rb', line 9 def add(word) key = "#{word[0,1].downcase}#{word.length}" @word_hash[key] ||= [] @word_hash[key] << word end |
.clear ⇒ Object
30 31 32 |
# File 'lib/omelettes/words.rb', line 30 def clear @word_hash = {} end |
.load(path = "/usr/share/dict/words") ⇒ Object
23 24 25 26 27 28 |
# File 'lib/omelettes/words.rb', line 23 def load(path="/usr/share/dict/words") clear (path.is_a?(Array) ? path : File.readlines(path)).each do |word| add(word.strip) end end |
.replace(word) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/omelettes/words.rb', line 15 def replace(word) key = "#{word[0,1].downcase}#{word.length}" valid_words = (@word_hash[key] || []) new_word = valid_words[rand(valid_words.size)] return new_word.send(word[0,1].upcase == word[0,1] ? :capitalize : :downcase) unless new_word.nil? word end |
.word_hash ⇒ Object
5 6 7 |
# File 'lib/omelettes/words.rb', line 5 def word_hash @word_hash ||= {} end |