class Ibsenphrase::Wordlist

Public Class Methods

new(filename) click to toggle source

A new wordlist object. It loads the file from disk on instantiation. @param filename [String] The filename without path. The file is looked

for in data/wordlists.
# File lib/ibsenphrase/wordlist.rb, line 6
def initialize(filename)
  @words = []
  load_words(filename)
end

Public Instance Methods

draw_word() click to toggle source

Picks a random word from the wordlist

@return [String] A word

# File lib/ibsenphrase/wordlist.rb, line 14
def draw_word
  @words.sample(random: Ibsenphrase::Random.prng)
end
num_words() click to toggle source

@return [Integer] Number of words in wordlist

# File lib/ibsenphrase/wordlist.rb, line 24
def num_words
  @words.size
end
words() click to toggle source

@return [Array<String>] All the words in the wordlist

# File lib/ibsenphrase/wordlist.rb, line 19
def words
  @words
end

Private Instance Methods

load_words(filename) click to toggle source
# File lib/ibsenphrase/wordlist.rb, line 30
def load_words(filename)
  path = File.join(Ibsenphrase.root, 'data', 'wordlists', filename)

  File.foreach path do |l|
    @words << l.strip
  end
end