Class: ETFC::Dictionary

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

Instance Method Summary collapse

Constructor Details

#initializeDictionary

Returns a new instance of Dictionary.



3
4
5
# File 'lib/etfc/dictionary.rb', line 3

def initialize
  init
end

Instance Method Details

#dictionaryObject

Private: Returns a dictionary

Checks for a words file and fallsback to a minimal dictionary en.wikipedia.org/wiki/Words_(Unix)



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/etfc/dictionary.rb', line 30

def dictionary
  if File.file?('/usr/share/dict/words')
    words_file('/usr/share/dict/words')
  elsif File.file?('/usr/dict/words')
    words_file('/usr/dict/words')
  else
    %w(
      this is a jury rigged dictionary banana monkey apple pear peach
      computers are so great robot dance
    )
  end
end

#initObject

Private: Memorize 20 words from the system dictionary



8
9
10
# File 'lib/etfc/dictionary.rb', line 8

def init
  @words = dictionary.sample(20).map(&:strip)
end

#randomObject

Public: Returns a random word

Example:

random
#=> 'mephitical'

Returns a random word (String)



20
21
22
23
24
# File 'lib/etfc/dictionary.rb', line 20

def random
  init if @words.empty?

  @words.pop
end

#words_file(words) ⇒ Object

Private: Retrieve all 3 to 9 letter words from a provided file

words - words file

Example:

words_file('/usr/share/dict/words')
#=> ['banana', ...]

Retruns a list of words



53
54
55
56
57
# File 'lib/etfc/dictionary.rb', line 53

def words_file(words)
  File.read(words).lines.select do |l|
    (3..9).cover?(l.strip.size)
  end
end