class ETFC::Dictionary

Public Class Methods

new() click to toggle source
# File lib/etfc/dictionary.rb, line 3
def initialize
  init
end

Public Instance Methods

dictionary() click to toggle source

Private: Returns a dictionary

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

# 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
init() click to toggle source

Private: Memorize 20 words from the system dictionary

# File lib/etfc/dictionary.rb, line 8
def init
  @words = dictionary.sample(20).map(&:strip)
end
random() click to toggle source

Public: Returns a random word

Example:

random
#=> 'mephitical'

Returns a random word (String)

# File lib/etfc/dictionary.rb, line 20
def random
  init if @words.empty?

  @words.pop
end
words_file(words) click to toggle source

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

# 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